Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The code produces an error: expected expression before ']' token

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>

int  main(){
    char myname[ ]="rahul";
    int myage=20;
    printf("My name is %s \n",myname);
    printf("My age is %d \n",myage);
    myname[ ]="sid";
    myage=40;
    printf("My name is %s \n",myname);
    printf("My age is %d \n",myage);
   return 0;
}
Posted
Updated 10-Aug-21 4:36am
v4

You can't assign a value to an array, you would need to either use strcpy or declare the array as a pointer. In this case because you are using string literals exclusively, i'd use a const char * instead of the array:
C
#include <stdio.h>
#include <stdlib.h>

int main(){
    const char *myname="rahul";
    int myage=20;
    printf("My name is %s \n",myname);
    printf("My age is %d \n",myage);
    myname="sid";
    myage=40;
    printf("My name is %s \n",myname);
    printf("My age is %d \n",myage);
    return 0;
}
If you use strcpy then you need to allocate enough array space when you initially declare myname to hold the longest string it is ever going to encounter or you app will either crash or give unpredictable results.
 
Share this answer
 
As Original Griff noted in solution 1, you need to use strcpy to assign a value to an array variable. What may not be quite clear is that you can initialize an array variable with a string.
In your case, you cannot have a space between the square brackets when defining an array e.g.
C
char first_name[] = "John";    /* valid */
char last_name[ ] = "Smith"; /* invalid syntax, space between [ and ] */

first_name = "Stephen"; /* Invalid: cannot assign to array" */
strcpy(first_name, "Stephen");  /* valid, but invokes undefined behavior */

In the above snippet of code, when we define first_name we get an array of 5 chars { 'J', 'o', 'h', 'n', '\0' }. Like any other C variable, it does not, and cannot change size, so if we call strcpy(first_name, "Stephen") we will assign the first 5 letters of "Stephen" to the storage reserved for first_name and the remaining letters will most likely overwrite other program variables - Though that is not necessarily the case - the situation invokes the dreaded "undefined behavior". It might be that your C compiler and library is smart enough to detect the overwrite and either truncate the assignment, or possibly even throw an exception, terminating the program. What happens in this case is defined in the standard as undefined. Compiler writers are free to do what they want when dealing with undefined behavior, possibly causing a system crash, formatting the system drives, or starting a global thermonuclear war!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900