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

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Arshitha
  • 33
  • 1
  • 8
  • 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] = "";` – Weather Vane Mar 13 '16 at 20:02
  • When allocating on the stack, you need to specify the size. You can allocate space on the heap without this restriction. – Filkolev Mar 13 '16 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 – Arshitha Mar 13 '16 at 20:13
  • @WeatherVane actually they are ill-formed (you can only have incomplete array at file scope) – M.M Mar 13 '16 at 20:29

3 Answers3

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.

Community
  • 1
  • 1
Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
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.

M.M
  • 138,810
  • 21
  • 208
  • 365
-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.

Shadowmak
  • 111
  • 13
  • 1
    "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. – Daniel Jour Mar 13 '16 at 20:09
  • 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: https://sketch.io/render/sk-56e5ca60321f5.png right? Or do I miss something? – Shadowmak Mar 13 '16 at 20:15
  • 1
    @Shadowmak for good practice you would use `first = malloc(sizeof *first *(SIZE_OF_ARRAY+1));` – Weather Vane Mar 13 '16 at 20:19
  • 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 Mar 13 '16 at 20:25
  • @Shadowmak not a quibble at all. In general, when you want to change to a different type, this is robust. – Weather Vane Mar 13 '16 at 20:31
  • Okay, I was thinking in small scale when usually u don't do this. But in general your solution is indeed better, thank you. – Shadowmak Mar 13 '16 at 20:33