Use of undeclared identifier when using struct? - c

When I try to assign char string_buffer in another struct it says
error use of undeclared identifier
I know that means it I need to declare string buffer in the current struct but is there a way without doing this.
struct ABC{
char string_buffer[64];
};
struct ABC *DEF(char *name){
name = string_buffer;
};

string_buffer is not a normal variable. It is a member variable of a variable of type struct ABC. You need to have a variable of type struct ABC and then, you need to access it usig the member access operator (. or ->), like
struct ABC sample = {0};
.... sample.name //valid access
Also, FWIW, based on your sample code, let me tell you, string_buffer is an array. You cannot assign the array the way you have shown in the sample snippet. In case you want to copy the content, you need to make use of strcpy().

Related

error: expression must have struct or union type in c when passing struct as param in function

I have such a struct
struct Request
{
char command[COMMAND_LENGHT];
char firstSetName[SET_NAME_LENGTH];
char secondSetName[SET_NAME_LENGTH];
char resultSetName[SET_NAME_LENGTH];
int input[sizeof(int) * 4];
};
and I have such a method
int parseToReadRequest(char * command, struct Request ** request)
{
printf("Command is : %s\n", command);
*request.firstSetName = "firs";
return 0;
}
As far as I understood when you need to pass a struct as a param you need to mark it with **
but anyway when I try to assign a struct value firstSetName I get an error
Expression must have struct or union type
What am I doing wrong?
EDIT
I get such error
In this expression:
*request.firstSetName = "firs";
The member access operator . has the highest precedence, so it assumes that request is a struct or union type. However it is not. It is a pointer-to-pointer-to struct.
If your intent is to modify an instance of a struct, you don't need a double pointer. Just a single will do:
int parseToReadRequest(char * command, struct Request *request)
Then, you would need to either put parenthesis around *request to ensure the pointer is first dereferenced before using .:
(*request).firstSetName = "firs";
Or you can use the pointer-to-member operator -> which is cleaner:
request->firstSetName = "firs";
However there is another issue here. You're attempting to assign a value to an array. An array can't be assigned to directly. What you want instead is to use the strcpy function which is used to copy strings:
strcpy(request->firstSetName, "firs");
You have to pass the structure by reference, ie. pass the pointer to it. You need to pass a double pointer only if you need to modify the pointer itself (for example dynamically allocate a structure inside the function).
The function should be: int parseToReadRequest(char * command, struct Request * request)
And inside the function you should access the structure fields like this: request->firstSetName

Is it OK to name a variable with the same name as a structure tag?

Is it OK to name a variable with the same name as a structure tag? For example:
struct sth {
char *a;
int b;
};
struct asdf {
struct sth sth; // is second sth OK??
};
Structure tags are in the 'tag' name space, shared with unions and enumerations. This is wholly separate from the 'ordinary identifier' name space (holding typedef names and variable names and function names), and also from the per-structure member names namespace.
That means you could have:
struct sth // DO
{ // NOT
int sth; // DO
} sth; // THIS!
This declares a structure variable sth of type struct sth with one member named sth.
The compiler can keep all that straight; you probably can't and probably shouldn't. In other words, although you can write code like that, it would be an incredibly bad idea to actually do so.

struct copy in c where struct are elements

Hi i have following situation
typedef struct
{
int a;
Name team[5];
Sport sport[5];
} School;
where Name and Sport are also structs,
typedef struct
{
char arry[20];
}Name;
typedef struct
{
char arry[20];
int tag;
}Sport;
then
School first_school, second_school;
I populate them individually, then at some point I do
first_school = second_school
But I step through code this line doesn't seem to work. How should I copy ?
But I step through code this line doesn't seem to work. How should I copy ?
It's entirely correct to copy struct like that
first_school = second_school; // valid
If it doesn't work as expected then the error is somewhere else. For example, you need to do strcpy() for string members.
Structures are values that can be assigned. They can contain arrays, which by themselves are not assignable, but being inside a struct makes it possible.
That code is fine, except you need to reverse the order of the declarations, since School references Name and Sport they must be declared first.
I tested it and it works just fine after reversing the declaration order, this prints hello:
int main(void) {
School foo, bar;
strcpy(bar.team[0].arry, "hello");
foo = bar;
printf("'%s'\n", foo.team[0].arry);
return 0;
}
There is probably something else wrong with your initialization of the second_shool, or you're failing to verify that it worked.
It will work for most members, but you have one that cannot be copied like that arry. You should copy one element at a time from the target to the destination instances.
Note that there are functions that take care of such copying like memcpy(). But you cannot copy an array by assignment like you do with an int or a struct actually.

Structure declaration with a pointer to it

I am studying structures in C from K&R book and encountered this:
struct{
int len;
char *str
} *p;
I am confused by this, because where the name of the struct variable should be, they have given a pointer *p. Can anyone please help me here? What does this declaration mean?
This declaration is a pointer to a struct which is composed of 2 fields — an int and a char*. This struct doesn't have a name and if you want to declare another pointer of the same struct, you will have to write it again.
Notice you can write something like this:
struct MyStruct {
int data1;
char data2;
};
This will define a new struct type which you can use later like this to declare a variable: struct MyStruct myVar;. The difference from what you wrote is that this struct doesn't declare a new variable but a new type since the struct in my example has a name and yours does not.
Another option is to use a typedef and give this struct a name and then you can use the name you have given it to declare more variables of that type.
You can read more about it at http://en.wikipedia.org/wiki/Typedef in the "Simplifying a declaration" section.

accessing struct: derefrencing pointer to incomplete type

When I'm trying to use and access the pointers to my structs i keep getting the annoying message of "dereferencing pointer to incomplete type" ....
For example in my user.h file I have this typedef:
typedef struct FacebookUser_t* User;
and in my user.c file which includes user.h I have this struct:
struct FacebookUser_t {...};
So when I need a pointer to that struct I only use User blabla;
it seems to work, and I add it to generic list as Element which is void* and that's the typedef for it in list.h:
typedef void* Element;
and when I get back a node from the list which contains Element (User) I can't access it's members, what I'm doing wrong? Thanks!
The problem is that the C file doesn't have access to the implementation of that strucure.
Try to move the definition of the structure in the header file.
If you want to hide the definition of a structure (by sticking the actual struct { block in a single C file and only exposing a typedefed name in the header, you cannot expect to access the fields directly.
One way around this is to continue with the encapsulation, and define accessor functions, i.e. you'd have (in user.h):
const char * user_get_name(const User user);
void user_set_name(User user, const char *new_name);
...
Please note that including the * in the typedef is often confusing, in my opinion.
As I understand it you try to access the members of User...
...via a Element, which is a void *. This can not work, because the compiler does not know what type it should dereference. For example:
int *intPtr = getIntPtr();
//Here the compiler knows that intPtr points to an int, so you can do
int i = *intPtr;
User *userPtr = getUserPtr();
//Here the compiler knows that userPtr points to an instance of User so you can do
User usr = *usrPtr;
//or access its member via ->
auto x = usr->someMember;
Element el = getElementFromSomewhere();
//el is of type void *!! void *can point to anything and everything
//the compiler has no clue what you want to do! So this both fails:
usr = *el;
el->someMember;
You first need to tell the compiler what your void * is pointing to. To do that you cast the pointer:
Element el = getElementFromSomewhere();
User *usrPtr = (User *)el;
I hope I understood your problem and this helps :)

Resources