1
char a[10];
scanf("%s",a);
int i=0;
while(a[i]!='\0')
     printf("\n%c",a[i++]);  //similar to printf("%s",a);               


char *b;
b=malloc(10*sizeof(char));
scanf("%s",b);
i=0;
while((b+i)!='\0')
     printf("\n%c",*(b+i++));   //not similar to printf("%s",a);

For input "abcd", the first loop prints a[] is it would be with printf(). But the same is not true for *b.

Second loops continues for too many until it encounters a '\0'.

So, does this mean '\0' is appended at the end of character strings automatically but not at the end of char type pointers?

And whose job is it to append this '\0'? Compiler's?

2
  • In this case it is scanfs responsibility to append \0. Commented May 7, 2012 at 16:02
  • Voting "too localized", since the questioner asked to delete it, and the problem is nothing to do with what the questioner thought it was to do with. The real question here is nothing to do with static/dymanic arrays, or anything else in the questioner's text. It's, "have I typo-ed (b+i) instead of b[i] in the second paragraph?", to which the correct answer is "yes" ;-) Commented May 7, 2012 at 16:07

2 Answers 2

7

You forgot to dereference the pointer you get with b+i. It should be:

while (*(b + i) != '\0') // or while (b[i] != '\0') or while(b[i])

b + i just gets you an address, you have to dereference it to actually look at what the memory is pointing at and see if it's the NUL-terminator. The x[y] notation is equivalent to *(x + y).

Also don't forget to free the memory you allocated with malloc.

Sign up to request clarification or add additional context in comments.

9 Comments

Also rudra note that b[i] is also valid.
@Joe yes definitely, added a note about that.
oops! please delete this question then!
@rudra the scanf function will append a NUL to the end of the string it reads. However, it is your job to make sure the array is large enough to hold the string scanf reads and the NUL terminator. By the way, you can give scanf the length of the string you want by putting a number between the % and the s, like scanf("%9s", b) where 9 is the maximum number of characters you want scanf to read. (I used 9 because your buffer is 10).
@rudra if you enter more than 10 characters, scanf will write beyond the end of your array. If you write (or read) beyond the end of an array, you get undefined behaviour which means anything can happen. It can appear to work (like it did for you), it can crash, it can set your hair on fire, or whatever else it wants. You can't tell. But it's still a bug in your program that you should be careful to avoid, because at another time or on someone else's computer or on some other platform, it might not behave the same way. Remember that C doesn't make sure you do things right.
|
1

Dereferencing issue.

In the line (b+i) should be replaced with either:

*(b+i)

or

b[i]

Additionally, if you are just taking in strings, you should consider using fgets() instead of scanf, as it can help you avoid the user typing in too many characters and crashing your program.

Finally, you might consider using calloc() instead of malloc() as it automatically sets the contents of the array you allocate to be all-zeros rather than random garbage.

As an example:

#include <stdio.h>
#define MAX_BUFFER_LENGTH
int main(void) {
  char a[MAX_BUFFER_LENGTH];
  char *b;
  fgets(a,MAX_BUFFER_LENGTH,stdin);
  int i=0;
  while(a[i]!='\0') {
     printf("\n%c",a[i++]);
  }

  b=calloc(MAX_BUFFER_LENGTH,sizeof(char));
  fgets(b,MAX_BUFFER_LENGTH,stdin);
  i=0;
  while(*(b+i)!='\0') {
    printf("\n%c",*(b+i++));
  }
  // Done
  return 0;
}

This is a much safer way to approach the problem you are solving.

Good luck!

2 Comments

For statically-sized buffers, scanf is ok because it lets you specify a maximum length to the string (although fgets would be a little better because you can use a constant in both fgets and the buffer size, instead of having to manually edit the string with scanf. But you can make a macro for that though...). calloc isn't really necessary either. +1 though.
Thank you. Agreed on both points. When using scanf, I tend to use fgets() and then sscanf as an extra degree of checking when I'm uncertain what the user might enter, coupled with a macro, as you described.