This is a bit written from memory so I apologize if I made a mistake in this posting. I created a struct and wanted to assign a name to it, but I get this error:
error: incompatible types in assignment of const char[3]' tochar[15]'
For the life of me I tried to understand what exactly is wrong here, I thought a constant char can still be assigned.
# include <stdio.h>
struct type{
char name[15];
int age;
};
main(){
struct type foo;
foo.name = "bar"; //error here
foo.age=40;
printf("Name- %s - Age: %d", foo.name, foo.age);
}
name is a fixed-size static buffer. You need to use strcpy or similar functions to assign it a string value. If you change it to be const char* name instead, then your code should work as-is.
char name[15]; declares an array, which is not assignable in C. Use string copying routines to copy the values, or declare name as a pointer - char* name; (here you'd have to worry about memory pointed to still being valid).
You can initialize a struct-type variable as a whole though:
struct type foo = { "bar", 40 };
Here string literal "bar" (four bytes including zero-terminator) will be copied into the name member array.
You need to use strcpy to copy content of strings.
He's confusing an initializer with an assignment.
Once the object is created (the "struct type foo;" line), you have to strcpy into "name").
struct type foo;
foo.name = "bar"; //error here <<= The compiler can only do a pointer assignment at this point, which is not valid.
==============
Don't write this crappy code:
strcpy_s(foo.name, 15, "bar");
The following allows you change the length in one place:
strcpy_s(foo.name, sizeof(foo.name), "bar");
Related
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'
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
So i'm learning structures and am trying inputting a string with dynamic memory allocation.
Here is what I have so far:
typedef struct{
char foo[81];
} TYPE;
void function(TYPE get[]){
char a[81];
scanf("%s", a);
get->foo = malloc(strlen(a)+1*sizeof(char)); //this line produces an error that it is not assignable
strcpy(get->foo,a);
return;
}
I'm not sure what is wrong with that statement, any help would be well appreciated.
foo is an array object and not a pointer so you can not use the operation
get->foo = (char*)calloc(strlen(a)+1,sizeof(char));
you are trying to assign a (void *) to type char[81], which is totally incompatible. Lvalue should be pointer in this case.
You have to declare you foo to be a char pointer.
typedef struct{
char * foo;
} TYPE;
You are trying to allocate memory for foo and assign it the address of the allocated memory. That won't work. foo is an array, and has already memory allocated to it (81 char elements.) And because it's an array, you can't assign an address to it.
What you want is to copy the a string to foo:
scanf("%s", a);
strcpy(get->foo, a);
However, you can do better by actually limiting the scan and copy operations to 81 characters, so that you won't write past the end of foo nor a:
fgets(a, 81, stdin);
strncpy(get->foo, a, 81);
Consulting the docs for fgets() and strncpy() is a good idea (there you can find out why you can specify 81 instead of 80 in the call to fgets().) You should always be careful not to write past the end of an array.
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.
My structure looks as follows:
typedef struct {
unsigned long attr;
char fileName[128];
} entity;
Then I try to assign some values but get an error message...
int attribute = 100;
char* fileNameDir = "blabla....etc";
entity* aEntity;
aEntity->attr = attributes;
aEntity->fileName = fileNameDir;
Compiler tells me:
Error: #137: expression must be a modifiable lvalue
aEntity->fileName = fileNameDir;
Why cant I assign here this character to the one in the structure?
Thanks
You're treating a char[] (and a char*, FTM) as if it was a string. Which is is not. You can't assign to an array, you'll have to copy the values. Also, the length of 128 for file names seems arbitrary and might be a potential source for buffer overflows. What's wrong with using std::string? That gets your rid of all these problems.
You're defining a pointer to some entity, don't initialize it, and then use it as if at the random address it points to was a valid entity object.
There's no need to typedef a struct in C++, as, unlike to C, in C++ struct names live in the same name space as other names.
If you absolutely must use the struct as it is defined in your question (it is pre-defined), then look at the other answers and get yourself "The C Programming Language". Otherwise, you might want to use this code:
struct entity {
unsigned long attr;
std::string fileName;
};
entity aEntity;
aEntity.attr = 100;
aEntity.filename = "blabla....etc";
You can't assign a pointer to an array. Use strncpy() for copying the string:
strncpy( aEntity->fileName, fileNameDir, 128 );
This will leave the destination not null-terminated if the source is longer than 128. I think the best solution is to have a bigger-by-one buffer, copy only N bytes and set the N+1th byte to zero:
#define BufferLength 128
typedef struct {
unsigned long attr;
char fileName[BufferLength + 1];
} entity;
strncpy( aEntity->FileName, fileNameDir, BufferLength );
*( aEntity->FileName + BufferLength ) = 0;
You should be copying the filename string, not changing where it points to.
Are you writing C or C++? There is no language called C/C++ and the answer to your question differs depending on the language you are using. If you are using C++, you should use std::string rather than plain old C strings.
There is a major problem in your code which I did not see other posters address:
entity* aEntity;
declares aEntity (should be anEntity) as a pointer to an entity but it is not initialized. Therefore, like all uninitialized pointers, it points to garbage. Hence:
aEntity->attr = attributes;
invokes undefined behavior.
Now, given a properly initialized anEntity, anEntity->fileName is an array, not a pointer to a character array (see question 6.2 in the C FAQ list). As such, you need to copy over the character string pointed to by fileNameDir to the memory block reserved for anEntity->fileName.
I see a lot of recommendations to use strncpy. I am not a proponent of thinking of strncpy as a safer replacement for strcpy because it really isn't. See also Why is strncpy insecure?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct st_entity {
unsigned long attr;
char fileName[FILENAME_MAX + 1];
} entity;
int main(void) {
int status = EXIT_FAILURE;
unsigned long attribute = 100;
char *fileNameDir = "blabla....etc";
entity *anEntity = malloc(sizeof(*anEntity));
if ( anEntity ) {
anEntity->attr = attribute;
anEntity->fileName[0] = '\0';
strncat(anEntity->fileName, fileNameDir, sizeof(anEntity->fileName) - 1);
printf("%lu\n%s\n", anEntity->attr, anEntity->fileName);
status = EXIT_SUCCESS;
}
else {
fputs("Memory allocation failed", stderr);
}
return status;
}
See strncat.
You're trying to use char* as if it was a string, which it is not. In particular, you're telling the compiler to set filename, a 128-sized char array, to the memory address pointed by fileNameDir.
Use strcpy: http://cplusplus.com/reference/clibrary/cstring/strcpy/
You can't assign a pointer to char to a char array, they're not compatible that way, you need to copy contents from one to another, strcpy, strncpy...
Use strncpy():
strncpy( aEntity->fileName, fileNameDir, sizeof(entity.fileName) );
aEntity.fileName[ sizeof(entity.fileName) - 1 ] = 0;
The strncpy() function is similar,
except that not more than n bytes of
src are copied. Thus, if there is no
null byte among the first n bytes of
src, the result will not be
null-terminated. See man page.
1) The line char* fileNameDir = "blabla....etc" creates a pointer to char and assigns the pointer an address; the address in this case being the address of the text "blabla....etc" residing in memory.
2) Furthermore, arrays (char fileName[128]) cannot be assigned to at all; you can only assign to members of an array (e.g. array[0] = blah).
Knowing (1) and (2) above, it should be obvious that assigning an address to an array is not a valid thing to do for several reasons.
What you must do instead is to copy the data that fileNameDir points to, to the array (i.e. the members of the array), using for example strncpy.
Also note that you have merely allocated a pointer to your struct, but no memory to hold the struct data itself!
First of all, is this supposed to be C or C++? The two are not the same or freely interchangeable, and the "right" answer will be different for each.
If this is C, then be aware you cannot assign strings to arrays using the '=' operator; you must either use strcpy() or strncpy():
/**
* In your snippet above, you're just declaring a pointer to entity but not
* allocating it; is that just an oversight?
*/
entity *aEntity = malloc(sizeof *aEntity);
...
strcpy(aEntity->fileName, fileNameDir);
or
strncpy(aEntity->fileName, fileNameDir, sizeof aEntity->fileName);
with appropriate checks for a terminating nul character.
If this is C++, you should be using the std::string type for instead of char* or char[]. That way, you can assign string data using the '=' operator:
struct entity {unsigned long attr; std::string fileName};
entity *aEntity = new entity;
std::string fileNameDir = "...";
...
entity->fileName = fileNameDir;
The major problem is that you declared a pointer to a struct, but allocated no space to it (unless you left some critical code out). And the other problems which others have noted.
The problem lies in the fact that you cannot just use a pointer without initialising it to a variable of that same datatype, which in this is a entity variable. Without this, the pointer will point to some random memory location containing some garbage values. You will get segmentation faults when trying to play with such pointers.
The second thing to be noted is that you can't directly assign strings to variables with the assignment operator(=). You have to use the strcpy() function which is in the string.h header file.
The output of the code is:
100 blabla......etc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
unsigned long attr;
char fileName[128];
} entity;
void main()
{
unsigned long int attribute = 100;
char *fileNameDir = "blabla....etc";
entity struct_entity;
entity *aEntity = &struct_entity;
aEntity->attr = attribute;
strcpy(aEntity->fileName, fileNameDir);
printf("%ld %s", struct_entity.attr, struct_entity.fileName);
}
For char fileName[128], fileName is the array which is 128 char long. you canot change the fileName.
You can change the content of the memory that filename is pointing by using strncpy( aEntity->fileName, fileNameDir, 128 );