Getting error while initializing structure variable? - c

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

Related

Strcpy from char* to char[] creating the wrong form

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.)

Repeating strings but not ints when reading from files

I've written a function that uses fscanf("%s &d &d &d") to read from a file. For some reason, when I display the output, all the integer values are correct, but the strings are all the same. food_stuff is a struct held in an array.
*EDIT
I've tried using a for to add each element from one array to the other, and I've tried strcpy.
Here's the function.
int readFromFile()
{
char*name;
int type;
int cals;
int price;
FILE * food_file;
food_file = fopen ("food_file.txt", "r");
while(fscanf(food_file, "%s %d %d %d", name, &type, &cals, &price)!=EOF)
{
food_stuff fs;
fs.name = name;
fs.type = type;
fs.calories = cals;
fs.price = price;
fflush(stdin);
addItem(fs);
}
}
As a commenter already has pointed out, you need to allocate memory for your variable name. Because the variable is temporary, you could just declare a local char array on the stack instead of allocationg memory on the heap:
char name[40];
Your problem is that the name field in your structure is probably also only a pointer to char. That pointer points to name for all footstuff structs you generate, which is the same for all instances, only with different contents. (What's worse: That buffer won't exist any more if you leave readFromFile, invalidating the name field for every foodstuff.)
One possible solution is to make the name field also a buffer, e.g:
typedef struct food_stuff food_stuff;
struct food_stuff {
char name[40];
int type;
int cals;
int price;
};
When you assign the read data to the struct, don't copy the pointer, but copy the contents with strcpy. (You need to include <string.h> for that):
strcpy(fs.name, name);
This solution assumes that all names are less than 39 characters long, which is not a safe assumption for real data. You should specify a width on the %s format in your call to fscanf.
Another solution is leave the struct definition as I assume it is now with char *name and to allocate memory for each new struct. You still have to copy the contents, though:
fs.name = malloc(strlen(name) + 1);
// error check omitted
strcpy(fs.name, name);
This solution requires that you free the extra memory allocated when you free the foodstuff structs.

C struct array assignment problems

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.

Error converting const char to char in struct?

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");

Structure Problem in C

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 );

Resources