C struct declaration and initialization - c

I have been following tutorials from the web on creating a struct and then initializing it in main(). From the tutorials I have followed, I have created my own example, which is as follows:
#include <stdio.h>
struct test {
int num;
};
main() {
test structure;
}
However, this does not work:
test.c: In function 'main':
test.c:8: error: 'test' undeclared (first use in this function)
test.c:8: error: (Each undeclared identifier is reported only once
test.c:8: error: for each function it appears in.)
test.c:8: error: expected ';' before 'structure'
But when I change:
test structure;
to:
struct test structure;
the code compiles. Why is this though? From the numerous examples I have looked at it seems that I shouldn't need the 'struct' before 'test structure'.
Thanks for your help/comments/answers.

You were reading C++ examples. In C the type of your structure is struct test not test.

You can get around that by doing
typedef struct test_s
{
int num;
} test;

Related

How to define a pointer to a structure

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

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.

Simple program won't compile in C

Okay I know right off the bat this is going to be a stupid question, but I am not seeing why this simple C program is not compiling.
#include <stdio.h>
#include <stdlib.h>
typdef struct CELL *LIST;
struct CELL {
int element;
LIST next;
};
main() {
struct CELL *value;
printf("Hello, World!");
}
I am new to C programming, not to programming in general, but to C. I am familiar with Objective-C, Java, Matlab, and a few others, but for some reason I can not figure this one out. I am trying to compile it using GCC in OS X if that makes a difference. Thanks for your help!
The error message I am getting is
functionTest.c:5: error: expected specifier-qualifier-list before ‘LIST’
functionTest.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’
Most importantly: You have misspelled typedef.
Then, at least these days, we normally add a return type to main, like so:
int main()
Also, main is supposed to return the exit status, so:
#include <stdio.h>
#include <stdlib.h>
typedef struct CELL *LIST;
struct CELL {
int element;
LIST next;
};
int main() {
struct CELL *value;
printf("Hello, World!\n");
return 0;
}
The main reason is that you typoed typedef as typdef. However, there are a couple other things you should do:
Add return 0; to the end of main().
Change the signature of main to int main(void)
Did you try to compile it with gcc -Wall -g yourprog.c -o yourbinary ?
I'm getting:
yourprog.c:3:8: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'struct'
yourprog.c:6:5: error: unknown type name 'LIST'
yourprog.c:8:1: warning: return type defaults to 'int' [-Wreturn-type]
yourprog.c: In function 'main':
yourprog.c:9:18: warning: unused variable 'value' [-Wunused-variable]
yourprog.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]
and you mispelled typedef and you should change the signature of main and add a return 0; inside.
By the way, I find your typedef very poor taste. I suggest to code (like Gtk does) something like typedef struct CELL CELL_t and declare CELL_t* value = NULL. because you really want to remember that value is a pointer to CELL_t. In particular, I hate typedef-s like typedef struct CELL* CELL_ptr; because I find very imporrtant (for readability reasons) to quickly understand what is a pointer and what is not a pointer.
Actually I would rather suggest
struct cell_st;
typedef struct cell_st cell_t;
cell_t *value = NULL;
(I do like initializing all pointers to NULL).
Add a return in your main function

How can we remove undeclared function error when function is declared with typedef in c?

When i have run the following piece of code:
typedef char *lrfield();
struct lrfields {
char name[26];
lrfield *f;
};
struct lrfields lr_table[] = {
{"pri_tran_code1", pri_tran_code2},
{"sec_tran_code", sec_tran_code},
{"type_code", type_code},
{"sys_seq_nbr", sys_seq_nbr},
{"authorizer", authorizer},
{"void_code", void_code},
{"",0}
};
char *pri_tran_code2()
{
return pri_tran_code;
}
*
*
if(second)
{
for(bp=lr_table; bp->name[0]; bp++)
if(strcmp(bp->name, second)==0)
{
tmpval=bp->f();
break;
}
}
I have got these errors:
error: `pri_tran_code2' undeclared here (not in a function)
error: initializer element is not constant
error: (near initialization for `lr_table[0].f')
error: initializer element is not constant
error: (near initialization for `lr_table[0]')
error: initializer element is not constant
error: (near initialization for `lr_table[1]')
As you can see in the code that i have defined 'pri_tran_code2' above its call. Please help me to solve this error.
Add char *pri_tran_code2(); before you mention this name? Or simply move the whole implementation there. It doesn't matter where you call it, what matters is where you refer to it.
Your declaration is erroneous. To declare a function (function-pointer) type, try this instead:
typedef char *(*lrfield)();

help with pointers, files and functions in c

throw me 3 errors when compiling I am using Dev-C + +.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct agenda{
char nombre [50];
char telefono[25];
char email[50];
}
struct nodo{
struct agenda dato;
struct nodo *proximo;
}
struct nodo *nuevonodo
int colavacia(struct nodo *)
struct nodo *creacola (struct nodo *, struct agenda);
void mostrar (struct nodo *);
void main()
{
struct nodo *pri=NULL, *ult=NULL;
struct agenda x;
printf ("ingrese nombre: ");
gets(x.nombre);
while (strcmpi(x.nombre, "fin"))
{
printf ("ingrese telefono: ");
gets (x.telefono);
printf ("ingrese mail: ");
gets(x.mail);
ult=creacola(ult,x);
if (pri==NULL) pri=ult; //si es la 1 pasada pongo en pri el valor del primer nodo
printf ("ingrese nombre: ");
gets(x.nombre);
}
if (colavacia(pri)==1)
{
printf ("No se ingresaron registros");getch();
}
else mostrar(pri);
}
struct nodo *nuevonodo()
{
struct nodo *p;
p=(struct nodo *)malloc(sizeof(struc nodo));
if(p==NULL)
{
printf ("memoria ram llena");
getch();
exit(0);
}
return p;
}
struct nodo *creacola(struct nodo *ult, struct agenda x)
{
struct nodo *p;
p=nuevonodo();
(*P).dato=x;
(*p).proximo=NULL;
if(ult!=NULL) (*ult).proximo=p; //si hay nodo anterior en prox pongo la direccion del nodo actual
return p;
}
int colavacia(struct nodo *pri)
{
if(pri==NULL) return 1;
else
return 0;
}
void mostrar (struct nodo *pri)
{
struct nodo *aux;
while(pri!=NULL)
{
printf("Nombre: %s - telefono: %s - Mail: %s \n",
pri->dato.nombre,pri->dato.telefono,pri->dato.mail);
aux=pri;
pri=(*pri).proximo;
free(aux);
}
getch();
}
You must end a struct definition with a semicolon:
struct agenda{
char nombre [50];
char telefono[25];
char email[50];
}; // <- HERE
Similarly for variable and function declarations:
struct nodo *nuevonodo;
int colavacia(struct nodo *);
And the return value of main has type int.
5587527.c:2:19: fatal error: conio.h: No such file or directory
I've commented out the offending #include and tried again
5587527.c:11:5: error: two or more data types in declaration specifiers
5587527.c:16:5: error: two or more data types in declaration specifiers
5587527.c:17:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
cc1: warnings being treated as errors
5587527.c:22:10: error: function declaration isn’t a prototype
5587527.c:22:10: error: return type of ‘main’ is not ‘int’
5587527.c: In function ‘main’:
5587527.c:22:10: error: old-style function definition
5587527.c:29:9: error: implicit declaration of function ‘strcmpi’
5587527.c:29:9: error: nested extern declaration of ‘strcmpi’
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
5587527.c:36:9: error: implicit declaration of function ‘creacola’
5587527.c:36:9: error: nested extern declaration of ‘creacola’
5587527.c:36:12: error: assignment makes pointer from integer without a cast
5587527.c:41:5: error: implicit declaration of function ‘colavacia’
5587527.c:41:5: error: nested extern declaration of ‘colavacia’
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: At top level:
5587527.c:61:18: error: conflicting types for ‘creacola’
5587527.c:36:13: note: previous implicit declaration of ‘creacola’ was here
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
I've added a ; to lines 10, 14, 16, 17 and tried again
cc1: warnings being treated as errors
5587527.c:22:10: error: function declaration isn’t a prototype
5587527.c:22:10: error: return type of ‘main’ is not ‘int’
5587527.c: In function ‘main’:
5587527.c:22:10: error: old-style function definition
5587527.c:29:9: error: implicit declaration of function ‘strcmpi’
5587527.c:29:9: error: nested extern declaration of ‘strcmpi’
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c:48:18: error: ‘nuevonodo’ redeclared as different kind of symbol
5587527.c:16:18: note: previous declaration of ‘nuevonodo’ was here
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
Fix main in line 22 (to int main(void))
5587527.c: In function ‘main’:
5587527.c:29:9: error: implicit declaration of function ‘strcmpi’
5587527.c:29:9: error: nested extern declaration of ‘strcmpi’
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c:48:18: error: ‘nuevonodo’ redeclared as different kind of symbol
5587527.c:16:18: note: previous declaration of ‘nuevonodo’ was here
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
Changed strcmpi in line 29 to strcmp
5587527.c: In function ‘main’:
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
cc1: warnings being treated as errors
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c:48:18: error: ‘nuevonodo’ redeclared as different kind of symbol
5587527.c:16:18: note: previous declaration of ‘nuevonodo’ was here
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
...
I've done a few correct errors / recompile / get more errors for you. Now it's your turn.

Resources