1
#include <stdio.h>

int main() {
    char prompt1[] = "Enter your first name:", prompt2[] = "Enter your last name:";
    char gratis[] = "Thanks!", first[], last[]; //empty declaration of string varible 

    printf(prompt1);
    scanf("%s", &first);
    printf(prompt2);
    scanf("%s", &last);

    printf("%s\n", gratis);
    printf("Your name is %s %s\n", first, last);
    return (0);
}

Why can't the string variable be declared without specifying the size of the char array? The same code works fine when the size of the array is mentioned.

screenshot of the error

4
  • 1
    first[] and last[] are of unknown size, so they can't be used for anything. Try specifying a length, and initialising. char first[100] = ""; Commented Mar 13, 2016 at 20:02
  • When allocating on the stack, you need to specify the size. You can allocate space on the heap without this restriction. Commented Mar 13, 2016 at 20:02
  • @WeatherVane I have mentioned at the end that it works fine when a size is specified. I wanted to know why it didn't work when size isn't specified Commented Mar 13, 2016 at 20:13
  • @WeatherVane actually they are ill-formed (you can only have incomplete array at file scope) Commented Mar 13, 2016 at 20:29

3 Answers 3

1

Arrays in C must have their size known when they are created, and the size cannot be changed after that.

So you must specify a size for all of your arrays.

You can omit the size number only if you provide an initializer, because the compiler can calculate the size from the initializer.

In your code you should select a size that is large enough to store what you expect. Also you used the wrong arguments to scanf. The code could be:

char first[100] = { 0 };    // avoid garbage in case input fails
scanf("%99s", first);

If you want to allow input of arbitrary size then you have to use dynamic allocation of space.

Sign up to request clarification or add additional context in comments.

Comments

1

In order for first and last to be usable in your context they need to have a complete type, that is a type whose size is known at compile time.

Thus you can either manually specify the array dimension (first[20]), or let the compiler deduce the dimension from an initialiser expression (like you did with prompt1 etc).

If you don't want to specify a dimension at compile time, then you need to switch to dynamically allocated memory using malloc and friends.

Note that you should also protect yourself from buffer overflows: scanf as used in your code will read an unknown amount of chars into the buffer you provide, making an overflow possible. See here for a discussion on how to avoid that.

Comments

-1

When you create array like this it is allocated to the stack and therefore you need to specify the size of it in the declaration. On the other hand you can use pointer and allocate the size of the array later:

The array in C is basically a pointer to the first element of the array.

You can create empty array declaring

char *first;

after that you can define the array using

first = malloc(sizeof(char)*(SIZE_OF_ARRAY+1));

+1 because you need to put ending \0 character at the end of the string.

Then you can work with it the same as with the array itself.

6 Comments

"array in C is basically a pointer to the first char of the array" no. An expression containing solely the array identifier is implicitly converted to a pointer to its first element ("the array decays to a pointer"). (I downvoted for that reason). Note also that sizeof(char) will always be 1.
Yes the sizeof(char) will be 1 but when you want to do the same with int it will be different. This is a good practice. To the other matter, I do not understand. If I draw it it should be like this: sketch.io/render/sk-56e5ca60321f5.png right? Or do I miss something?
@Shadowmak for good practice you would use first = malloc(sizeof *first *(SIZE_OF_ARRAY+1));
Now you are just quibble. I assumed you know the type of the array. This aims to save the developer from accessing unallocated memory because he didn't know the size of the element correctly.
@Shadowmak not a quibble at all. In general, when you want to change to a different type, this is robust.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.