user1658865
user1658865

Reputation: 81

New to C... struct can't find a variable I identified

I'm new to this whole C thing, but I keep getting this error with my code

UArray2.c:19:error: request for member ‘i’ in something not a structure or union

It's obviously the uarray.i in my main function, but I don't get why it isn't seeing it.

This is my .h file. Not too interesting...

//UArray2.h
#include <stdlib.h>
#include <stdio.h>
#ifndef UARRAY2_INCLUDED
#define UARRAY2_INCLUDED
#define T UArray2_T
typedef struct T *T;

#undef T
//#undef UARRAY2_INCLUDED //undef?                 
#endif

This is my .c file. Pretty simple stuff.

//UArray.c                                              
#include <stdlib.h>
#include <stdio.h>
#include "UArray2.h"
#define T UArray2_T

struct T{
     int i;
};

int main()
{
     UArray2_T uarray;
     uarray.i=0;
     return 0;
}
#undef T

So, does anyone have any idea as to why I'm getting this compile error? It's likely something stupid that I did.

Upvotes: 1

Views: 302

Answers (2)

Iti Tyagi
Iti Tyagi

Reputation: 3661

I think there is a problem with the initialization as you are using the pointer in the header file.

    typedef struct T *T;

You are actually pointing to the memory location by declaring uarray. Try to rectify this error.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

In the header file you have

typedef struct T *T;

This means that when you declare the variable uarray you are actually declaring a pointer. So you should initialize the i member as

uarray->i = 0;

This will however most likely crash, as the pointer is uninitialized and can point to any location in memory. Either allocate memory for the pointer

UArray2_T uarray = malloc(sizeof(*uarray));

Or make it point to another structure

struct UArray2_T real_uarray;
UArray2_T uarray = &real_uarray;

Upvotes: 4

Related Questions