Home > Net >  Defining strings using pointers Vs. char arrays in C
Defining strings using pointers Vs. char arrays in C

Time:11-19

I am confused about how pointers to characters work. when I run the following code, what happens?

int main()
{
    char* word;
    scanf("%s",word);
    printf("%s",word;
}

the first line in the main is defining a pointer to char without initialization. scanf should store the word somewhere and give the address to the pointer, right? what if I input a big string, would it overwrite something in the memory?

And what happens in the first line in the following code other than defining a pointer to char. Does the compiler set some limits? or I can't exceed the size specified, right? If done, I will have a run time error, right? what is the difference between the two cases?

int main()
{
    char word[100];
    scanf("%s",word);
    printf("%s",word;
}

What about pointers to other types? Can I just keep writing to the following places using offsets?

CodePudding user response:

scanf should store the word somewhere and give the address to the pointer, right?

No. It is the other way around. You define the address where scanf shall store the value. As you fail to initialize the pointer to some valid address, you cause undefined behaviour that might result in a crash in best case or seem to work in worst case.

And what happens in the first line in the following code other than defining a pointer to char.

There is no pointer involved at all. An array is not a pointer. An array provides all the memory it needs to store all its members. A pointer doesn't do this.

Does the compiler set some limits? or I can't exceed the size specified, right?

You can write wherever you want. No one will prevent you from doing this. At least no from trying. If you write to some location that does not belong to the memory you allocated, you again cause undefined behaviour.

CodePudding user response:

According to the description of the conversion specifier %s in the C Standard

If no l length modifier is present, the corresponding argument shall be a pointer to the initial element of a character array large enough to accept the sequence and a terminating null character, which will be added automatically.

That is when you pass a pointer as an argument of the function that corresponds to the format %s it shall point to the first element of a character array where the input string will be stored. The character array shall be large enough to accommodate the entered string (including the appended terminating zero character '\0')

In the first program

int main()
{
    char* word;
    scanf("%s",word);
    printf("%s",word;
}

the pointer word is uninitialized and has an indeterminate value. So these two statements

    scanf("%s",word);
    printf("%s",word;

invoke undefined behavior.

You need to provide a valid value of the pointer that will point to a character array. For example

char s[100];
char *word = s;

Or you can allocate memory dynamically like

char *word = malloc( 100 * sizeof( char ) );

In the second program

int main()
{
    char word[100];
    scanf("%s",word);
    printf("%s",word;
}

the array word used as an argument is implicitly converted to a pointer to its first element. If you will enter a string that fits in the array with 100 elements then the program will behave correctly.

However if you will enter 100 or more characters without embedded spaces then the program again will have undefined behavior.

To avoid such a situation you can specify the maximum length of the string that can be read in the array word by using the length modifier the following way

    scanf("           
  • Related