0

Why dont we use '&' in the case of strings in function scanf ??

e.g. :-

scanf("%s",date);

here date is a character arraymeans string.

Plz help

3
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by deceptikon
0

dont know, maybe you can play with this code i wrote using printf and figure it out, it will be fun

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
   int * piInteger = NULL;
   int iInteger = 0;
   void *vPointer = NULL;
   char *pcBuffer = {"xyz"};//3 bytes and null terminate
   printf("Value of pointer [%d]\n", piInteger);
   //printf("Seg faul cause points to null , go where points",*piInteger);
   printf("iInteger value is %d, bot address %x \n",iInteger,&iInteger);
   piInteger = &iInteger;//piInteger adress now gets adress of iInteger
   printf("was seg fault before, now %d\n",*piInteger);
   //bot also we can say differently
   memcpy(&iInteger, pcBuffer, sizeof(pcBuffer)) ;//we copy 4 byte buffer string to 4 byte char
   printf("new value of int %d bot piInteger points to it too look %d\n",iInteger, *piInteger);
   printf("treat it as address to null terminated char array %s and this one too %s",piInteger, &iInteger);
      vPointer = pcBuffer;
   printf("%s",vPointer);

   return 0;
}

Edited by Sokurenko: fix

2

Why dont we use '&' in the case of strings in function scanf ??

e.g. :-

scanf("%s",date);

here date is a character arraymeans string.

Because date is already a pointer in this case. There's no need to add another level of indirection with the address-of operator.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.