How to define a pointer to a structure - c

I know this is a very basic problem, but I cannot move forward without it and its not clearly explained elsewhere.
Why is this programming giving me so many errors of undeclared identifier? I have declared it, though.
These are the error i am getting.
Error 2 error C2143: syntax error : missing ';' before 'type'
Error 3 error C2065: 'ptr' : undeclared identifier
Error 4 error C2065: 'contactInfo' : undeclared identifier
Error 5 error C2059: syntax error : ')'
Error 15 error C2223: left of '->number' must point to struct/union
and more...
#include<stdio.h>
#include<stdlib.h>
typedef struct contactInfo
{
int number;
char id;
}ContactInfo;
void main()
{
char ch;
printf("Do you want to dynamically etc");
scanf("%c",&ch);
fflush(stdin);
struct contactInfo nom,*ptr;
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));
nom.id='c';
nom.number=12;
ptr->id=nom.id;
ptr->number=nom.number;
printf("Number -> %d\n ID -> %c\n",ptr->number,ptr->id);
}

typedef struct contactInfo
{
int number;
char id;
}ContactInfo;
This code defines 2 things:
a type named ContactInfo
a struct named contactInfo
Notice the difference of the c and C!
In your code you are using a mixed combination of both, which is allowed (although confusing IMHO).
If you use the struct variant you need to explicitly use struct contactInfo. For the other variant (ContactInfo) you must to omit the struct part as it is part of the type definition alteady.
So be careful with both different definitions of your structure. Best would be to only use either one of the variants.
I do not have Visual Studio at hand, but the following (corrected) code compiles with gcc properly without any warnings:
#include<stdlib.h>
typedef struct contactInfo
{
int number;
char id;
}ContactInfo;
void main()
{
ContactInfo nom,*ptr;
ptr=malloc(2*sizeof(ContactInfo));
}
(I left out the not so interesting/unmodified parts of the code)

This:
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));
is wrong, there's no type called contactInfo.
There's a struct contactInfo, which is typedef:d as ContactInfo. C is case-sensitive (and you must include the struct unlike how it works in C++).
Also note that the quoted line is better written as:
ptr = malloc(2 * sizeof *ptr);
Since casting the return value of malloc() is a bad idea in C, I removed the cast. Also, repeating the type is a bad idea since it makes the risk of introducing error greater, so I removed that as well.
Note that sizeof *ptr means "the size of the the type that ptr points at" and is a very handy thing.

C is a case sensitive language.
ptr=(contactInfo)malloc(2*sizeof(contactInfo));
which should be:
ptr=malloc(2*sizeof(ContactInfo));

Replace
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));
with
ptr=malloc(2*sizeof(struct contactInfo));

struct contactInfo nom,*ptr;
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));
here you are using contactInfo to typcast where as it should be struct contactInfo. and as you have typedef it into ContactInfo you can use that also.
struct contactInfo nom,*ptr;
ptr=(ContactInfo*)malloc(2*sizeof(ContactInfo));

Related

Issue initialising values for typedef field in struct - C

I'm currently learning C. I've been playing around with typedef and structs, and ran into a strange error (at least to my inexperienced eyes).
I am using a typedef to create a dimensions type (int array of two values), and I have a struct that uses that type def.
Upon trying to specify values for the field in my main, I run into an error:
error: expected expression before ‘{’ token
The code:
typedef int dimensions[2];
struct television
{
dimensions resolution;
};
int main()
{
struct television theTV;
theTV.resolution = {1024, 768};
return 0;
}
It's a very contrived example -- is it possible to initialise the .resolution variable in this way?
Use instead:
struct television theTV = {{1024, 768}};
{} initializer list can only be use in a declaration and cannot be used in a statement.
You are not allowed to use assignment to an array, since it is a non-modifiable l-value. However, you can use memcpy() with a compound literal:
memcpy(theTV.resolution, (dimensions){1024, 768}, sizeof(dimensions));

C Compiler Pointless Errors?

I have the following code written in C:
n. struct UDSData {
char *name;
char *address;
};
n. char UDS1[16] = "fill up sixteen", UDS2[16] = "fill up sixteen";
n. while (something) {
...
108. char UDS1Temp[16], UDS2Temp[16];
109. strcpy(UDS1Temp, UDS1);
110. strcpy(UDS2Temp, UDS2);
111.
112. struct UDSData item = {UDS1Temp, UDS2Temp};
113. UDSCodes[UDSTotal++] = item;
}
Any idea why the code compiles to give these errors:
1><file>(112): error C2143: syntax error : missing ';' before 'type'
1><file>(113): error C2065: 'item' : undeclared identifier
1><file(113): error C2440: '=' : cannot convert from 'int' to 'UDSData'
Removing the strcpy() and inputting UDS1 and UDS2 directly into the struct works.
You are almost certainly using an early compiler standard, like C89, which does not allow mixed declarations and code. You need to declare item near the start of the code block. Something like this:
char UDS1Temp[16], UDS2Temp[16];
struct UDSData item = {UDS1Temp, UDS2Temp};
strcpy(UDS1Temp, UDS1);
strcpy(UDS2Temp, UDS2);
UDSCodes[UDSTotal++] = item
Since you are only placing the pointers into the struct, the initialisation can be done before the strcpy. But you must declare UDSData after the two char arrays.

errors while trying to pass a structure to a function

In the following program I try to pass a structure to a function. But I get errors,and I do not understand why. What mistake have I made in this program ?
I am using gcc for compiling this c program.
#include <stdio.h>
struct tester {
int x;
int *ptr;
};
void function(tester t);
int main() {
tester t;
t.x = 10;
t.ptr = & t.x;
function(t);
}
void function(tester t) {
printf("%d\n%p\n",t.x,t.ptr);
}
Errors :
gcc tester.c -o tester
tester.c:8:15: error: unknown type name ‘tester’
tester.c: In function ‘main’:
tester.c:12:2: error: unknown type name ‘tester’
tester.c:13:3: error: request for member ‘x’ in something not a structure or union
tester.c:14:3: error: request for member ‘ptr’ in something not a structure or union
tester.c:14:13: error: request for member ‘x’ in something not a structure or union
tester.c: At top level:
tester.c:18:15: error: unknown type name ‘tester’
NOTE : If I replace printf with cout and stdio with iostream and name the extension to .cpp (!), I get no errors. Why is that ? No wonder I compile it using g++
If you don't typedef the struct you must specify struct in front of the struct name while declaring it like so:
struct tester t;
Either you do that or you do the following:
typedef struct {
int x;
int *ptr;
}tester;
Update
Below is a quote from Adam Rosenfield from the following post Difference between 'struct' and 'typedef struct' in C++?:
In C++, all struct/union/enum/class declarations act like they are implicitly typedef'ed, as long as the name is not hidden by another declaration with the same name.
your struct isn't named. either use struct tester t; or usa a typedef
The thing is you're trying to compile with gcc, which is a "c language" compiler and you're following C++ style of code.
It is possible to create a struct variable by just structname variablename;
but in c++, you explicitly have to tell the compiler it is a struct like
struct structname variablename;
Just do that and you'll be fine, otherwise you can use a typedef which is basically you tell the compiler form now on you are going to call struct tester to only tester, which will suit you more since you only have to only a minor change.

Illegal use of type as expression

I have the following lines in my source code which is giving the error
error C2275: 'HPDF_Array' : illegal use of this type as an expression"
:
This is the actual code in the header file:
typedef struct _HPDF_Array_Rec *HPDF_Array;
typedef struct _HPDF_Array_Rec {
HPDF_Obj_Header header;
HPDF_MMgr mmgr;
HPDF_Error error;
HPDF_List list;
} HPDF_Array_Rec;
HPDF_Array id;
How do I resolve this error?
This code works on my compiler (gcc 4.7.1), so maybe you need to show more code or information in general if you're still having trouble:
// content of this struct is irrelevant, so I just made a dummy struct
struct _HPDF_Array_Rec {
int dummy;
};
typedef struct _HPDF_Array_Rec *HPDF_Array;
HPDF_Array id;
What the code in your question does, is define the type HPDF_Array as a pointer to the type struct _HPDF_Array_Rec and instantiate one HPDF_Array (which is a pointer to a _HDPF_Array_Rec struct) called id
EDIT:
The code still works fine. I googled the errorcode and got this:
http://msdn.microsoft.com/en-us/library/76c9k4ah(v=vs.71).aspx
An expression uses the -> operator with a typedef identifier.
Check your code for that kind of error. Anywhere you're doing HPDF_Array->something instead of id->something ? :)
If you have error while compiling hpdf_pdfa.c file, then move
HPDF_Array id;
line to the beginning of the HPDF_PDFA_GenerateID(HPDF_Doc pdf) function.
It appears that 2.2.1 branch was built as a C++ code, and the committer didn't notice the error. In the trunk branch this error is fixed.

recurrent declare in c

typedef void (callback)(int *p1, sStruct *p2);
typedef struct _sStruct
{
callback *funct;
}sStruct;
I have the following declaration, in C. How can I compile this recurrent declaration without receiving any error ?
For the moment I receive: syntax error before '*' token on first line.
You can forward-declare the structure:
/* Tell the compiler that there will be a struct called _sStruct */
struct _sStruct;
/* Use the full name "struct _sStruct" instead of the typedef'ed name
"sStruct", since the typedef hasn't occurred yet */
typedef void (callback)(int *p1, struct _sStruct *p2);
/* Now actually define and typedef the structure */
typedef struct _sStruct
{
callback *funct;
} sStruct;
Edit: Updated to match the question's change of type names.
Also, I strongly suggest that you do not give the struct the identifier _sStruct. Global names beginning with a _ are reserved names, and using them for your own identifiers could cause undefined behavior.

Resources