0
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char *str=malloc(sizeof(char)*100);
    int length=0;
    printf("Enter string :\n");
    scanf("%c",str);
    while(*str)
    {
        length++;
        *str++;
    }
    printf("%d",length);
    return 0;
}

I'm trying to write a program to find length of string using pointers.But whatever the string, I'm getting the result as 1. Can somebody tell me what's wrong?

12
  • 3
    %c reads a single character. Use %s. Commented Feb 28, 2019 at 18:13
  • also, str++, not *str++ Commented Feb 28, 2019 at 18:13
  • @federico-klez-culloca I think that's fine. ++ has higher precedence than * Commented Feb 28, 2019 at 18:14
  • Welcome to the wonderful world of undefined behavior. Commented Feb 28, 2019 at 18:15
  • 1
    With your current code there might not be a null-terminator. So the incrementing of the pointer will make you use uninitialized parts of the memory. And the contents of that memory is indeterminate (and should be seen as almost random or "garbage"). That could lead you to go out of bounds. Commented Feb 28, 2019 at 18:39

3 Answers 3

2

You allocate 100 bytes ok

char *str=malloc(sizeof(char)*100);

int length=0;
printf("Enter string :\n");

You have a string but read one character

scanf("%c",str);

While that character is != 0 You increment the character with one e.g. 'A' becomes 'B' and so on the character overflows

while(*str)
{
    length++;
    *str++;

Instead, read a string using fgets()

const int maxlen = 100;
char *str=malloc(maxlen); 

if (fgets(str,maxlen,stdin) != NULL)
{
  // now to calculate the length
  int length = 0;
  char* p = str;  // use a temp ptr so you can free str 
  while (*p++) 
  { 
    ++length; 
  }
  printf("length=%d", length);
  free(str); // to avoid memory leak
}
Sign up to request clarification or add additional context in comments.

Comments

1

The %c modifier in scanf reads character sequences. As you did not provide a field width, it reads by default only one character per time. You might want to use the %s modifier.

Further, when no length modifier is added, the returned character sequence is not null terminated, which makes your loop to determine the length risky (you also might want to use the strlen function from the C standard library, but this function also expects a null terminated sequence).

Comments

0

Problem is your scanf.

char *str=(char*)malloc(sizeof(char)*100);

printf("Enter string :\n");
scanf("%s",str);
int i = 0;
for (i = 0; i < 100 && str[i] != '\0'; i ++)
{
}
printf("%d",i);

3 Comments

Don't cast the result of malloc. And add a length-modifier to the format so the buffer won't be overrun. And always check what scanf returns.
If I do not cast the malloc some compiles would give me an error. I tried with GCC 8.2. I did not try to optimize OPs code, just make it run. But you are generally right - C and memory needs some attention to be safe.
Then you're using C++ and not C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.