0

I read many previous questions but none of them cleared my doubt.
When I define and initialize a pointer as

int a = 10;
int* p;
p = &a;
printf("%d", *p); // this will print the value of 'a' towards which p is pointing 

But when I use scanf statement like-

int *p;
scanf("%d", &p);
printf("%d", p); // is this form of input similar to the one above?

Also when I use char pointer to read a string-

char* name[10];
scanf("%s", name);
printf("%s", name); // runs correctly.

What I know is that scanf expects pointer as input (like &a if it's like int a;)
But If I use--

char* names[5][10];
scanf("%s", names[1]); // reading the first name. Is this correct? because length of name can vary.

Now I am unable to print this, I tried no of ways.
A detailed explanation would be appreciated, my teacher isn't that good.
DOUBTS

  • When do we use * with a pointer? i.e. to print its value or for what?

  • Unable to scan char* xyz[a][b];

  • A brief explanation of my mistakes and the code above.
    Edits-

    int* arr[n];
    for(int i =0; i<n; i++){
    printf("Enter the salary of %d person:", i+1);
    scanf("%d", &(arr[i]));
    printf("\n");
    }

Also, this type of assignment of value is not right?

  • 1
    There are few parallels between the working `printf()` and the `scanf()` code, which should fail horribly. You're asking the user to enter a valid address as a decimal number, assuming that `sizeof(int *) == sizeof(int)` which is usually not the case on 64-bit systems. You're then printing the pointer entered using an inappropriate format. It's hard to know why you think there are useful analogies between the two. – Jonathan Leffler Jan 04 '21 at 06:02
  • The `char *name[10];` defines an array of pointers; you've not defined any storage that those pointers point at. Reading a string while passing the name of the array of pointers is completely bogus. You annotate that it runs correctly – that's surprising, but not actually impossible. It is, however, undefined behaviour and that means anything can happen, including appearing to work. But it is 100% unreliable. – Jonathan Leffler Jan 04 '21 at 06:05
  • try with float pointer and %f... – Antti Haapala -- Слава Україні Jan 04 '21 at 06:05
  • 1
    The `char* names[5][10];` example is still weirder. You should be using a compiler that complains bitterly about the mismatches between your format strings and the variables passed to `scanf()`. If your compiler doesn't complain (when tweaked with suitable arguments to increase the warning levels), then you shoulds get a better compiler. – Jonathan Leffler Jan 04 '21 at 06:06
  • @JonathanLeffler So I should only limit the use of pointers to store the memory address not anything else. Actually, we have some assignments in which they ask us to only use pointers. I use gnu gcc compiler. I should not store values in pointers directly using `scanf` ryt? –  Jan 04 '21 at 06:14

1 Answers1

0
   printf("%d", p); // is this form of input similar to the one above?

no, %d expects an int argument, you're passing an int *. Supplying mismatched argument type for a conversion specifier invokes undefined behaviour.

That said, in case of

char* name[10];      // array of character pointers!
scanf("%s", name);
printf("%s", name); // runs correctly.

you're going wrong. Check the data types. %s expects the argument to be a pointer to a char array, so your code should be

char name[10];        // array of characters
scanf("%9s", name);  // mandatory error check for success to be done.
printf("%s", name);

as, in most of the cases including this one, an array type decays to the pointer to the first element of the array, so while passing as function argument, name is actually of type char *.

Same goes with

char* names[5][10];
scanf("%s", names[1]);

changing that to

char names[5][10];

will suffice.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Can you explain the last point a bit more? If I am getting it right the reference variable `name` is of the type `char*`. –  Jan 04 '21 at 06:17
  • @PrithvirajYadav Which part? array decay? you can check [What is array to pointer decay?](https://stackoverflow.com/q/1461432) – Sourav Ghosh Jan 04 '21 at 06:21
  • You don't seem to get the point: **Check the data types!** – Sourav Ghosh Jan 04 '21 at 06:29
  • No I got the point, I am just saying, that is also not correct. –  Jan 04 '21 at 06:29
  • I got what you were saying about format specefier –  Jan 04 '21 at 06:30
  • They why do you write (in your latest example) `int* arr[n];` and not `int arr[n];`? See, all format specifiers have a required type, ensure you supply that proper type and nothing else. – Sourav Ghosh Jan 04 '21 at 06:30
  • I was just confirming that it is incorrect :-) –  Jan 04 '21 at 06:32