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

Recommended Answers

All 2 Replies

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;
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.