-2

I wrote a program to do these tasks : First get a string from user and calculate the size of it with sizeof() function. Here is my code :

#include <stdio.h>
int main()
{
    char U1[];
    puts("Enter a string:");
    scanf("%s", U1);
    printf("The %s has %i bytes.", U1, sizeof(U1));
    return 0;
}

The compilation error is : error: array size missing in ‘U1’ Why ? Please explain what is wrong here ?

InJecTable
  • 53
  • 1
  • 1
  • 7

5 Answers5

4

You need to define array size to tell the compiler how much space to allocate:

char U1[256];

If you don't know the size of your array at compile time, you can dynamically allocate memory using malloc:

// #include <stdlib.h>
int *arr;
int n, i;
printf("Number of elements: ");
scanf("%d", &n);

// Allocate n ints
arr  = malloc(n * sizeof(int));

printf("Enter %d elements: ", n);
for(i = 0; i < n; i++)
   scanf("%d", &arr[i]);

printf("Here they are: ");
for(i = 0; i < n; i++)
   printf("%d ", arr[i]);

// Free the array on the end!
free(arr);

Note

printf("The %s has %i bytes.", U1, sizeof(U1));

will always print 256, as sizeof returns the size of the array deducted at compile time, not the number of the characters just read into the array. You could use sizeof(char) * (strlen(U1) + 1) to compute number of bytes required by string (+1 comes from NUL-terminator character at the end of the string).

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
4

C arrays are fixed-length, therefore every array needs to have its size fully specified when it is declared. A valid declaration is something like:

char U1[256];

Note that if the user enters more than that number of characters (minus one for the terminating zero), the behavior of that program is undefined. To avoid this problem, replace scanf("%s") with fgets() and learn about dynamic arrays.

Community
  • 1
  • 1
user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • No if you enter 257 byte string it will overflowed . Am I right ? How can I prevent that ? – InJecTable Sep 17 '13 at 13:28
  • @InJecTable Even if you enter a 256-byte string, it will overflow the array - because C strings are terminated with a null character. To avoid that, replace a single `scanf` with multiple calls to `fgets`, and use a dynamically allocated array, as mentioned in the answer. – user4815162342 Sep 17 '13 at 14:06
1

Arrays in C needs to be declared with size. The other option is to use dynamic arrays which are pointers

sheu
  • 284
  • 5
  • 13
0

You need to give a size to your array. Scanf takes an already allocated array and it do not resizes it, it just fill it with the input.

Your code should look like this:

#include <stdio.h>
int main()
{
    char U1[256];
    puts("Enter a string:");
    scanf("%s", U1);
    printf("The %s has %i bytes.", U1, strlen(U1));
    return 0;
}

Notice that I did not use sizeof, because I would have the entire size of the array, not just the string. strlen will give you the size of a C null-terminated string.

Jupotter
  • 384
  • 2
  • 15
0

you should make a certain array, normal C compile do not support the dynamic array.

王玉林
  • 9
  • 3