1

I am little confuse about output and error shown by compilation of below programs.

Case 1 In this case I have not fixed the size of array of variable of char. But assign a value during declaration and it works easily. And I have read that we can't use assignment operator in case of array. But here it works. Don't know how.

#include <stdio.h>

int main()
{
    char str[] = "programming"; //dynamic size of array

    printf("%s", str);
    /*It works. Array size is dynamic as well as array bounded variable with char data type is not assignable but it works here. Don't know how? */
}

Case 2 This case is similar as above but value is not assigned during declration of variable and It's not work.

#include <stdio.h>

int main()
{
    char str[]; //dynamic size of array

    str = "programming";
    printf("%s", str);
    /* It's not work. Error shown that " definition of variable with array type needs an explicit size or an initializer" */
}

Case 3; This is the real error about I have learned that array type char is not assignable.

#include<stdio.h>

int main()
{
    char str[50];//fixed array size
    
     str="programming";
     printf("%s",str);
     //It's not working. 
     //compilor output is " array type 'char [50]' is not assignable"
     /*I understand it. It's correct error. but I don't understand error in other case.*/
}

Case 4; in this case I have assigned a value to variable with array but It doesn't work.

#include <stdio.h>

int main()
{
    char str[50];

    str[11] = "programming";
    printf("%s", str);
    //It's not work.
    /*Error by compilor is "assigning to 'char' from incompatible type 'const char [12]'
         str[11]="programming" */
}
2
  • 3
    You're confusing string initialization with string assignment. Strings can be used as initializers for char arrays, but they may not be assigned to arrays. If you want to copy a string to an array, use strcpy. Commented May 3, 2021 at 6:38
  • 1
    Arrays in C are never assignable. Syntax '[]' mean the the compiler should infer the size from array's initializer. Commented May 3, 2021 at 6:41

1 Answer 1

1

you are confused.

    char str[] = "programming"; //dynamic size of array

This doesn't mean str is dynamically sized. This means that str is sized according to the length of the string literal you have used to initialize it. It's size is (and will be always you call the scope it is included in) the string length you have declared it (in this case 12 characters)

Case 2 is not also some way of declaring an array and not giving it a size. This will make you to get an error from the compiler. As written

    WhateverType a[];

is an incomplete object definition (the array lacks a length as in the first case) so you'll get an error from the compiler.

Case 3 and Case 4 are the same. In this case you don't provide an initializer, but you specify an array length. The object is complete and you don't get an error. But this never means that arrays are dynamic size. Dynamic means you can change it at runtime. Something like this:

int f(char *s)
{
    char array[strlen(s)];
}

In this case (illegal, except in last standard, or implemented as a gcc extension, for example) each time you call f(), you get a different array size, from the calculation of the length of string s, passed to the function as a parameter. This is not permitted in many C implementations, as the array size is not known until you call the function. In this case, the array length is not statically defined at compilation time.... and this means the local amount of storage this function will require has to be calculated at each function call, and not a constant, as in the previous cases. This doesn't mean it is impossible, but it requires some computation to be put at the start of the function call. And this is something the inventors of C didn't want.

This notation is some kind of syntactic glue to allow you to define an array of characters by providing an initializer (so you don't have to count the number of chars in the string literal to dimension the array) But it never means that you can call that function with a different size or to use a different array size next time (see that you have used a constant expression to define it --this is, one that can be calculated at compilation time, not each time you enter into the function)

On other side, an array is NEVER assignable. Not a char array, but and array of whatever cell type you can define. There's no way in C in which you can refer to an array as a whole object. Arrays are decayed to pointers when passed as arguments, and if you use the array name in one expression, it means the address of the first element (which is different from the address of the array) This is made on purpose in the original C specification to disable large array copies in a single C statement, so final machine code is very simple, and efficient. It's my opinion, but this forces programmers to think twice and to consider the drawbacks of copying large amounts of memory (and how it slows the machine) without necessity and slow down the program. Others think of it as some way to make C a low level language in which no aggregate operations are possible. Well, that is true, but it also gives C part of its beauty.

In the last case (Case 4) you provide an array length and an initializer. This means that the array length will be obeyed, and the string literal will be truncated to fit in the array. It nevers means the array length is dynamic.

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

3 Comments

Thanks a lot sir. I got your points and I have no more doubts about the dynamic size of array because size is not dynamic (in case 1), it's fixed because compilor infer the size of array from initializer and it's get fixed.
Beware that the last C standard I think allows to declare a local array dynamically (this means, on procedure activation) to have a different size on each function call. At least CLANG and GCC allow this kind of declarations (although the array size is the same for all the duration of the function call)
Got it sir. I will try the last c standard concept that you have described and analyse the result.

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.