Home > Enterprise >  Why do I need "&" in printf when I want to print a pointer
Why do I need "&" in printf when I want to print a pointer

Time:08-28

So I wrote this code where I scan 2 strings. One is declared as an array and one as a pointer. Now to my question: Why do I need for printing text2 in the printf-statment the "&" before Text2 and when I print Text1 not?

I thought if I put "&" in printf before the variable it pirnts the memory address. I this case not, it prints the string.

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

int main()
{
    char Text1[45];
    char *Text2;

    
    scanf("%s" , &Text1);
    printf("Text1: %s\n", Text1);

    scanf("%s" , &Text2);
    printf("Text2: %s\n", &Text2);


    return 0;
}

CodePudding user response:

char Text1[45] is an array of characters. The compiler will allocate 45 bites in a program memory for use by the program. The value of the bytes is not known at the moment. So, scanf("%s" , Text1) will put input chars into this memory, assuming that there are less than 44 of them, or it will override the program stack and possibly crash. To prevent from this issue, you should use something like Ds.

There is no need to use & in this case. It does not do much with the array declared in such a way. Therefore you do not need it in printf("%s\n", Text1). But you can use it if you wish.

char *Text2 declares a pointer variable. It means that the compiler allocates enough space to contain the pointer value. The value of the pointer is not defined at the moment, so it does not point anywhere. If you plan to use it with characters, you need to allocate space for them or assign the space in a different way. For example, Text2 = malloc(45) will allocate 45 bytes for use and set a pointer to those bytes. Or you can do Text2 = Text1, assigning address of the first byte of the Text1 array as a pointer. This way the Text1 array will be used as a byte storage.

As a result, scanf("%s", Text2) will use the pointer to access bytes, either allocated by malloc or in the Text1. Now you need to printf("%s\n", Text2).

You should not use & on Text2. It will return an address of the pointer variable and not the address of the array of bytes. You need the latter. So print with &Text2 will return trash and could cause a crash.

BTW, if you used malloc it is a good idea to free the memory which was allocated if it is not needed any longer: free(Text2).

CodePudding user response:

Let's get rid of the part dealing with Text1 for the moment, and focus solely on Text2. That leaves us with something like this:

    char *Text2;

    scanf("%s" , &Text2);
    printf("Text2: %s\n", &Text2);

You've declared Text2 as a pointer, but you haven't initialized it to point to any available space. Then you pass the address of that pointer to scanf, and match it up with a format that tells scanf to read a string, and deposit it at the specified location, so instead of using the pointer as a pointer, scanf will try to use it as if it were an array of char.

To make this work sanely, we want to use the pointer as a pointer, and have it point at some available memory--and we want to tell scanf the size of that memory, so the user can't enter more data than we've provided space to store.

#define MAXSIZE 128

char *Text2 = malloc(MAXSIZE);

scanf("7s", Text2); // note lack of ampersand here

printf("%s\n", Text2); // Now we don't need an ampersand here either.
  • Related