I'm new to C and I am working with a struct array. I'm having trouble assigning values to it. here is my struct..
struct student{
char firstname[30];
char surname[30];
int streetNo;
char streetName[30];
char suburb[30];
char state[4];
int postCode;
char DOB[10];
int studentNo;
char gender;
char courseNo[4];
char active;
int WAM;
};
struct student person[1000];
here is me assigning a value to the struct
person[100].firstname = "dan";
and this is my error
assignment type mismatch: array[30] of char "=" pointer to char
You can only initialize array like that at the time of declaration only, else you need to use
strcpy(person[100].firstname,"dan");
you can't even do like that with a simple char array
char a[30];
a="dan";
the complier will tell :
incompatible types when assigning to type ‘char[30]’ from type ‘char *’
because "dan" is a string literal that is being held by a pointer which can't be assigned like this to an array.
Although you can initialize an array of characters from a string literal like this
char str[] = "dan";
you cannot assign a string literal to an array of characters the way you are trying to do.
You need to copy your string into the character array using one of the string copy functions:
strcpy(person[100].firstname, "dan");
If yo would like to copy "dan" into the first four elements and pad the remaining elements of firstname with zeros, use strncpy:
strncpy(person[100].firstname, "dan", 30);
It is worth pointing out that you could make firstname a pointer, and either allocate memory for your strings dynamically, or assign it directly:
struct student{
char *firstname;
char *surname;
/* and so on... */
};
student[100].firstname = "dan";
student[100].surname = "brown";
In C, a string like this is implemented using a character array. A character array is exactly what you have defined, but it is not possible to assign a string directly like this in C. You will have to use string functions for that. The function to use is strcpy(). You have to assign like:-
strcpy(person[100].firstname, "dan");
Arrays are not pointers, and this is one example of it.
Arrays cannot be assigned to, only array elements can be assigned to.
You could do
person[100].firstname[0] = 'd';
person[100].firstname[1] = 'a';
person[100].firstname[2] = 'n';
person[100].firstname[3] = '\0'; /* Pretty tedious... */
or, if you know that you don't copy more than 30 bytes,
strcpy (person[100].firstname, "dan");
An array name itself gives array base address.. And an array base address cannot be a left side value.
person[100].firstname =
gives you error since you are assigning some other value to array base address which is not allowed.
You can initialize
char stringArray[] = "some string";
but you can't assign value to already declared array
char stringArray[100];
stringArray = "some string"; <== error
You alternative is to use strcpy
strcpy(stringArray, "sometext");
The specific problem is that you are attempting to assign a string to an array of bytes.
What you need to do in that particular case is to copy the contents of the string you want into the array, like so:
strncpy(person[100].firstname, "dan", 30);
A more general problem is what you are doing is terrible. For student records like this, the only sensible thing to do is to use a proper database; in your case using SQLite is probably appropriate. Using a database for a simple learning exercise like so might seem like overkill, but it is experience that'll help you out a lot later.
You can strcpy() for assigning value to char array.
strcpy(person[100].firstname,"Dan");
You can't assign like this to an array. Even without a struct. That is
char name[10];
name = "ert";
is an error.
(You can do it only in initialization char name[10] = "ert";)
The correct way to do it is
strcpy(person[100].firstname, "dan");
Safer to use a variation of strcpy that requires a max size of string.
Related
typedef struct Symbol{
char varName[16];
} Symbol;
...............
Symbol *newSymbol = malloc(sizeof(Symbol));
const char space[2] = " ";
char *String = "Name Test";
//break off the first word from String and put it into name
char *name;
name = strtok(String,space);
//convert the char * to char[16]
char nameArray[16];
strcpy(nameArray,name);
//set newSymbol->varName to the newly created char[16]
newSymbol->varName = nameArray
I have a char * called String. In my actual program, it is read from a file using fgets, I am just calling it "Name Test" for the purposes of this example. I want to take the first word of the string and assign it as the varName in a Symbol. So what should happen is newSymbol->varName is set to "Name". Because strtok returns a char * but I need a char[16] for the struct, I must convert the char * to a char[16].
However, I get this error:
"Error: incompatible types when assigning to type 'char[16]' from type 'char*'
newSymbol -> varName = nameArray;
So, it seems like strcpy it not actually converting the char * to a char[16]. Even after declaring a char[16] and telling strcpy to put the contents of the char * into it, I still have a char * instead of a char[16]. I need to make it work without changing the struct, so that is not an option here.
How can I convert a char * into a char[16]?
You cannot assign the contents of an array using the regular assignment operator in C.
You can use strcpy for strings and memcpy/memset for other data types. (You could use memcpy/memset for strings too but strcpy is simpler)
Instead of
newSymbol -> varName = nameArray;
use
strcpy(newSymbol -> varName, nameArray);
So, it seems like strcpy it not actually converting the char* to a char[16].
No, the problem is that C does not provide for assigning to (whole) arrays. newSymbol->varName is an array of 16 char. You can assign to elements of that array, and you can copy into it with strcpy() or a similar function, but you cannot assign to the whole array.
In your particular code, I'd dispense with variable nameArray, changing this ...
strcpy(nameArray,name);
... to this:
strcpy(newSymbol->varName, name);
(Or perhaps to a similar usage of strncpy(), to protect from overrunning the array bounds.)
The program is as follows
typedef struct Signal {
long int vr[4];
char name[4];
char Type;
char casuality[2];
};
and I wanted to use this structure in such way where for eg: for variable vr I am able to do vr[0]=1073741824 but for casuality if I assign casuality[0]="output" it is showing error as given above
void xmlRead()
{
struct FMU *fmu;
struct Signal *var;
struct Signal iname;
(*var).vr[0]=1073741824;
(*var).vr[1]=1073741825;
(*var).vr[2]=1073741826;
(*var).vr[3]=1073741827;
(iname).name[0]="Ball1_pos"; //Here it is showing the error//
}
In your case, "output" is a string literal, having type char [7] and casuality[0] is of type char.
Obviously, they are not compatible (one is an array, the other a simple char) and hence the error.
There are two ways to achieve what you want,
Change char casuality[2]; to char *casuality[2]; then the assignment will work fine (however, you can't modify a string literal).
Allocate an array long enough and use strcpy() to copy the content of the string literal into the array.
You are trying to assign a string literal (made of several chars), to the first location of an array of four chars (Signal.name[0]).
// a single char = a string literal, i.e., from const char* to char
(iname).name[0] = "Ball1_pos";
This is obviously incompatible in types.
Note that even if the string literal was short enough to be fit into the memory spanned by the variable name, you would need a function such as strcpy or memcpy to assign the value to that memory.
In particular, something like the following would work:
strcpy(iname.name, "abc")
(note that string literals are null terminated by the compiler and strcpy includes the \0)
To make things (hopefully) more clear for you, the following would be equivalent:
iname.name[0] = 'a'
iname.name[1] = 'b'
iname.name[2] = 'c'
iname.name[3] = '\0'
struct command
{
char *abc[1000];
};
One of the variable is defined in a structure. This char pointer array contains the value after reading the value from file. For example {123,121}.
Now I want to cast these value in array of array of char, something like this:
char a1[][1000]= s1->abc[j];
I am not sure about the syntax... So how should I cast these values in array abc in a1.
If I understand correctly you have an array of 1000 char*, that might have different length (in your example, length is 2). And s1 type is struct command.
Then I think you meant:
char a1[1000][] = s1->abc;
Or:
char a1[] = s1->abc[j];
But I am not sure why you would want to do that instead of:
char * a1[1000] = s1->abc;
Or
char ** a1 = s1->abc
Be careful with indexes though...
I am confused about why I am getting an error while initializing a structure variable. I have found a few related threads on Stack Overflow, but they did not solve my problem. Can anyone explain what is causing the problem?
//I have used Dev-C++ compiler
//Getting an error 'b1' undeclared
struct book
{
char name ;
float price ;
int pages ;
};
struct book b1;
int main()
{
/* First i have tried following way to initialize the structure variable,
but compiler throws the error
b1 = {"John", 12.00, 18};
*/
//I have tried to initialize individually ....but still getting error...?
b1.name = "John";
b1.price = 12.3;
b1.pages = 23;
printf ( "\n%s %f %d", b1.name, b1.price, b1.pages );
system("pause");
return 0;
}
Problems
only one byte memory available for name field. use like char name[100]
use strcpy() instead of b1.name = "John";
You clearly want a string variable in your struct, but you declared it as char name; (a single character).
Instead, use a char pointer (const char *name), or a char array (char name[100]). In the latter case, you will have to use strcpy(b1.name, "John").
The problem is that you have declared name as a char. That means name will hold only a single char, not an entire string.
You can statically allocate name as an array of char:
char name [25] // or whatever length you think is appropriate
// remember to leave room for the NUL character
Then you will need to use strcpy() to copy the actual name into the structure.
Alternatively, you can declare name as a pointer to char:
char* name;
In this case, you can set the pointer to a string literal:
name = "John";
But in most real life situations, this is not an option, as you will be reading the name in from a file or standard input. So you will need to read it into a buffer, set the pointer to dynamically allocated memory on the heap (the string length of the buffer + 1 for the NUL), and then use strcpy().
You have to do some tweaks to your code in order to make it work.
char name; // only one byte is allocated.
Define char array to store strings in C.
char name[20]; //stores 20 characters
char *name; //Pointer variable. string size can vary at runtime.
Once you have created a object for your structure than you can feed data only using that object.
(i.e) object.element=something;
b1 = {"John", 12.00, 18}; // Not possible
The above initialization is possible only while defining objects.
struct book b1={"John", 12.00, 18}; //Possible
If char *name is defined inside struct, you can do the following.
struct book b1;
b1.name="John"; // Possible
b1.price=12.00; // Possible
b1.pages=18; // Possible
If you use char array char name[20] then you can do the following.
struct book b1;
strcpy(b1.name,"John"); // Possible
b1.price=12.00; // Possible
b1.pages=18; // Possible
Why is the following code illegal?
typedef struct{
char a[6];
} point;
int main()
{
point p;
p.a = "onetwo";
}
Does it have anything to do with the size of the literal? or is it just illegal to assign a string literal to a char array after it's declared?
It doesn't have anything to do with the size. You cannot assign a string literal to a char array after its been created - you can use it only at the time of definition.
When you do
char a[] = "something";
it creates an array of enough size (including the terminating null) and copies the string to the array. It is not a good practice to specify the array size when you initialize it with a string literal - you might not account for the null character.
When you do
char a[10];
a = "something";
you're trying to assign to the address of the array, which is illegal.
EDIT: as mentioned in other answers, you can do a strcpy/strncpy, but make sure that the array is initialized with the required length.
strcpy(p.a, "12345");//give space for the \0
You can never assign to arrays after they've been created; this is equally illegal:
int foo[4];
int bar[4];
foo = bar;
You need to use pointers, or assign to an index of the array; this is legal:
p.a[0] = 'o';
If you want to leave it an array in the struct, you can use a function like strcpy:
strncpy(p.a, "onetwo", 6);
(note that the char array needs to be big enough to hold the nul-terminator too, so you probably want to make it char a[7] and change the last argument to strncpy to 7)
Arrays are non modifiable lvalues. So you cannot assign to them. Left side of assignment operator must be an modifiable lvalue.
However you can initialize an array when it is defined.
For example :
char a[] = "Hello World" ;// this is legal
char a[]={'H','e','l','l','o',' ','W','o','r','l','d','\0'};//this is also legal
//but
char a[20];
a = "Hello World" ;// is illegal
However you can use strncpy(a, "Hello World",20);
As other answers have already pointed out, you can only initialise a character array with a string literal, you cannot assign a string literal to a character array. However, structs (even those that contain character arrays) are another kettle of fish.
I would not recommend doing this in an actual program, but this demonstrates that although arrays types cannot be assigned to, structs containing array types can be.
typedef struct
{
char value[100];
} string;
int main()
{
string a = {"hello"};
a = (string){"another string!"}; // overwrite value with a new string
puts(a.value);
string b = {"a NEW string"};
b = a; // override with the value of another "string" struct
puts(b.value); // prints "another string!" again
}
So, in your original example, the following code should compile fine:
typedef struct{
char a[6];
} point;
int main()
{
point p;
// note that only 5 characters + 1 for '\0' will fit in a char[6] array.
p = (point){"onetw"};
}
Note that in order to store the string "onetwo" in your array, it has to be of length [7] and not as written in the question. The extra character is for storing the '\0' terminator.
No strcpy or C99 compund literal is needed. The example in pure ANSI C:
typedef struct{
char a[6];
} point;
int main()
{
point p;
*(point*)p.a = *(point*)"onetwo";
fwrite(p.a,6,1,stdout);fflush(stdout);
return 0;
}