error: request for member ‘age’ in something not a structure or union

This is a discussion on error: request for member ‘age’ in something not a structure or union within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> struct Employee{ int a; char name[32]; int age; }; main(int argc, char *argv[]){ ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    error: request for member ‘age’ in something not a structure or union

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    struct Employee{
    	int a;
    	char name[32];
    	int age;
    };
    
    main(int argc, char *argv[]){
    	struct Employee **buf;
    	FILE *infile;
    	int i, size;
    
    		size = atoi(argv[1]);
    		buf = (struct Employee **)malloc(size*sizeof(struct Employee *));
    		for(i=0; i<size; i++){
    			buf[i] = (struct Employee *)malloc(32*sizeof(struct Employee));
    		}
    
    		buf[2].age = 4;
    		
    	return 0;	
    }
    where is the error? i can' t find it...can somebody help me...

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    987
    buf is a pointer to pointer to struct. Thus buf[2] is a pointer to a struct. The structure member operator (the dot) only works on structs, not pointers to structs. You want the structure pointer operator (->), which works on pointers to struct: buf[2]->age = 4;

    This sort of access doesn't quite make sense given that you've allocated 32 structs for each element of your array, though. I mean, it's not wrong, per se, but semantically it'd make more sense to do something like:
    Code:
    buf[0][0].age = 4; /* or */
    buf[size - 1][15].age = 4;
    /* etc */
    since what you effectively have is an array of arrays (aka a 2d array). buf[2]->age is the same thing as buf[2][0].age, but the latter makes more sense given how you've set things up.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    See it as every buf[n] contains another, smaller array, with m elements. So to access an actual struct, you need to access buf[x][y] where x, y > 0 and x < n, y < m.
    And you're not freeing your memory at the end.
    Oh and main shall have a return type of int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Let me see if i got it...

    1) I make an array named buf which has 2 dimensions [size][32]...

    2) Each 32 cells (in the second dim) include a struct, meaning that it includes 3 "variables" (an array and 2 int variables)...

    Right ?

    Problem (in the above code):

    I tried to access the variable "age" using '.' but i used only one dimension (buf[size]) instead of two!

    Sorry for writting the same things as you wrote but i wanted to confirm that my thoughts are right...
    Last edited by brack; 09-04-2010 at 09:16 AM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Yes, that looks right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-18-2010, 04:14 AM
  2. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  3. union inside structure
    By vijay s in forum C Programming
    Replies: 1
    Last Post: 12-03-2009, 06:21 AM
  4. structure and union
    By BEN10 in forum C Programming
    Replies: 10
    Last Post: 06-24-2009, 11:30 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21