Struct problem... Can anyone help?

This is a discussion on Struct problem... Can anyone help? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> struct Player{ int x; }; int main(void){ int i,j; struct Player **A; struct Player data; ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    81

    Smile Struct problem... Can anyone help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Player{
    	int x;
    };
    
    int main(void){
    	int i,j;
    	struct Player **A;
    	struct Player data; // line 11
    	
    	A=(data **)calloc(5, sizeof(data *));
    		for(i=0; i<5; i++){
    			A[i]=(data *)calloc(3, sizeof(data));	
    		}
    		
    		for(i=0; i<5; i++){
    			for(j=0; j<3; j++){
    				struct Player[i].x=4; // line 21
    				//data[i].x = 4; // line 22
    			}
    		}
    		
    		for(i=0; i<5; i++){
    			printf("\n");
    			for(j=0; j<3; j++){
    				printf("Array[%d][%d]: %d", i, j, data[i].x);
    			}
    		}
    	return 0;
    }
    My problem is in lines: 11, 21, 22...(in these lines i am not sure if the code is right...it's my second day in structs...)
    I'm not sure though if the rest is ok...

    output:

    vasileios@vasileios-laptop:~/desktop$ gcc askisi1.c
    askisi1.c: In function ‘main’:
    askisi1.c:13: error: expected expression before ‘)’ token
    askisi1.c:15: error: expected expression before ‘)’ token
    askisi1.c:20: error: expected identifier or ‘(’ before ‘[’ token
    askisi1.c:28: error: subscripted value is neither array nor pointer
    Last edited by nullifyed; 05-21-2010 at 04:27 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think you want "A" here (the actual instance) instead of "struct Player" (the name of a datatype):
    Code:
    		for(i=0; i<5; i++){
    			for(j=0; j<3; j++){
    				struct Player[i].x=4; // line 21
    				//data[i].x = 4; // line 22
    			}
    		}
    What is the purpose of struct Player data?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    81

    Apart from "A"?

    It is very simple...i just wanna try to learn about structs....so, i do some examples...small programs...apart from "A" as you said???

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay. When I wrote "I think you want A here" I did not mean there was a choice, sorry. What you have is incorrect. Consider:

    Code:
    struct Player **A;
    The blue part is a datatype. The green part is a variable. This:
    Code:
    struct Player[i].x=4;
    is called a subscript. Datatypes are not variables, hence it makes no sense to use a subscript on them. An analogy would be:
    Code:
    int array[32];
    int[4] = 12;
    Do you see what is wrong with using "int[4]"?

    Probably you should try a 1D dynamic array of structs before you try a 2D one.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    81

    Answer!

    Yes, i think you 're right...i' ll try a 1-d array...and yes again i got that int[i] is WRONG....i fact it is a HUGE mistake for a programmer...but i am not yet... thx!!!

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    81

    Smile Making it 1-dim!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Player{
    	int x;
    };
    
    main(){
    	int i;
    	struct Player *A; // Initializing an array one-dim named A
    	
    	A=(struct Player *)calloc(5, sizeof(struct Player)); // Making an dynamic array (calloc)
    	
    	for(i=0; i<5; i++){
    		A[i].x=4;
    	}
    		
    	for(i=0; i<5; i++){
    		printf("Array[%d]: %d", i, struct Player.x); // Printing... 
    	}
    return 0;
    }
    So, i made it one dimension...but the compiler 'hit' the last printf

    Output:

    root@vasileios-laptop:/home/vasileios/desktop# gcc askisi1.c
    askisi1.c: In function ‘main’:
    askisi1.c:19: error: expected expression before ‘struct’

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's A[i].x, A[i].x, A[i].x. Still. Hasn't changed since line 15, as sneaky as the C grammar rules can be, they're not that fast.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    81

    Answer

    So? What should i put?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by nullifyed View Post
    So? What should i put?
    A[i].x. Still.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You made the same mistake. Would you print int.x? No, it makes no sense. You need to print the .x of some object.

    You seem to be unclear of the difference between an instance and a type.

    Also:
    Code:
    int main(void)
    is a correct definition of main.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  4. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  5. Replies: 16
    Last Post: 10-29-2006, 05:04 AM

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