error w/ pointer to structures

J

Jeremy Beasley

I'm having a problem using pointers to structures. One method of declaring
point and then referencing a structure works, while the other generates an
error at compile time.

-----------------
ex1. (this method works)

struct data {
int a,b;
} myData;

struct data *p_data = &myData; /* define pointer and reference on
line */
--------------------------
ex2. (this method gives me a compile time error)

struct data {
int a,b;
} myData;

struct data *p_data; /* declare pointer of type data */
p_data = &myData; /* reference pointer to struct myData */
------------------------------

ex2. does the same thing as ex1. put just splits it apart on separate lines.
When compiling ex2., I get the following errors:
"conflicting types for 'p_data', previous declaration of 'p_data', data
definition has no type or storage class"
 
K

Karthik Kumar

Jeremy said:
I'm having a problem using pointers to structures. One method of declaring
point and then referencing a structure works, while the other generates an
error at compile time.

You have not mentioned it, but it seems like these lines do not
belong to *any* function in the source code. i.e. they are available
globally.
-----------------
ex1. (this method works)

struct data {
int a,b;
} myData;

struct data *p_data = &myData; /* define pointer and reference on
line */

Ok - because you define a variable and initialize it right-away then.
--------------------------
ex2. (this method gives me a compile time error)

struct data {
int a,b;
} myData;

struct data *p_data; /* declare pointer of type data */

You are defining it. Fine.

p_data = &myData; /* reference pointer to struct myData */

Not Fine, since the global variable cannot come in the l.h.s here.
If you want to do this, do it inside any function.


<-- Code compiles file -->

#include <stdlib.h>

struct data {
int a,b;
} myData;

struct data *p_data;

int main() {
p_data = &myData; /* reference pointer to struct myData */
system("pause");
return EXIT_SUCCESS;
}
 
J

Jack Klein

I'm having a problem using pointers to structures. One method of declaring
point and then referencing a structure works, while the other generates an
error at compile time.

-----------------
ex1. (this method works)

struct data {
int a,b;
} myData;

struct data *p_data = &myData; /* define pointer and reference on
line */
--------------------------
ex2. (this method gives me a compile time error)

struct data {
int a,b;
} myData;

struct data *p_data; /* declare pointer of type data */
p_data = &myData; /* reference pointer to struct myData */
------------------------------

ex2. does the same thing as ex1. put just splits it apart on separate lines.
When compiling ex2., I get the following errors:
"conflicting types for 'p_data', previous declaration of 'p_data', data
definition has no type or storage class"

The second method will work inside a function, but not at file scope.

Your first example is a definition with initialization, which is
allowed at file scope as long as the initial data is compile-time
constant.

The second example is a definition with default initialization, which
is 0 initialization if static and no initialization if automatic.
This is then followed by an assignment statement. An assignment
statement, or any other executable statement, can only appear inside a
function.
 
A

Allin Cottrell

Jack said:
The second method will work inside a function, but not at file scope.

To illustrate Jack's point -- you should find that the following does
not generate any error:

struct data {
int a,b;
} myData;

int main (void)
{
struct data *p_data;
p_data = &myData;

return 0;
}

Allin Cottrell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,834
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top