Thread: Dynamic Struct Array: base operand of '->' has non-pointer

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    12

    Dynamic Struct Array: base operand of '->' has non-pointer

    I can't find the problem to this test.
    I've already allocated memory space for the array, and I can't access to it from the pointer by using '->'

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define TOTAL 3
    
    
    int main() {
    
    
    	typedef struct {
    		char name[20];
    		int age;
    	} Person;
    
    
    	Person *persons;
    
    
    	persons = (Person *) malloc(TOTAL * sizeof(Person));
    
    
    	strcpy(persons[0]->name = "Ray");
    	persons[0]->age = 4;
    	printf("%s, %d", persons[0]->name, persons[0]->age);
    
    
    	/*
    	for (int i=0; i < TOTAL; i++) {
    		printf("// %d\n", i);
    		printf("name: "); scanf("%20s", persons[i]->name);
    		printf("age: "); scanf("%d", &persons[i]->age);
    		printf("- - - -\n\n");
    	}
    
    
    	for (int i=0; i < TOTAL; i++) {
    		if (persons[i]->age > 21) {
    			printf("%s, %d anios.\n", persons[i]->name, persons[i]->age);
    		}
    	}
    	*/
    
    
    	free(persons);
    		
    	fflush(stdin);
    	getchar();
    	return 0;
    }
    This is my output:

    Code:
    D:\arbol\documentos\qwerty\taller lenguaje 1\asdf\struct_malloc.c: In function 'int main()':
    D:\arbol\documentos\qwerty\taller lenguaje 1\asdf\struct_malloc.c:17:19: error: base operand of '->' has non-pointer type 'main()::Person'
    D:\arbol\documentos\qwerty\taller lenguaje 1\asdf\struct_malloc.c:18:12: error: base operand of '->' has non-pointer type 'main()::Person'
    D:\arbol\documentos\qwerty\taller lenguaje 1\asdf\struct_malloc.c:19:29: error: base operand of '->' has non-pointer type 'main()::Person'
    D:\arbol\documentos\qwerty\taller lenguaje 1\asdf\struct_malloc.c:19:47: error: base operand of '->' has non-pointer type 'main()::Person'
    [Finished in 0.3s with exit code 1]

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I've already allocated memory space for the array, and I can't access to it from the pointer by using '->'
    That's correct because you're not trying to access the element with a pointer. You're using array[] notation so you need to use the dot (.) operator instead.


    Jim

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    12
    Quote Originally Posted by jimblumberg View Post
    That's correct because you're not trying to access the element with a pointer. You're using array[] notation so you need to use the dot (.) operator instead.


    Jim
    But "persons" is a pointer, am I wrong??

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    Person * persons;
    Persons is a pointer. What does it point to? In your case, an allocated piece of sequential memory big enough to fit 'TOTAL' Person objects.
    More simply persons points to a Person object, which is not a pointer.

    So what do you think persons[0] is? It's the first Person object that persons is pointing to. As we just discussed above, Person object is not a pointer it's a scalar. So you would use dot notation as Jimblumberg suggested.

    If you wanted to allocated 'TOTAL' pointers to Person objects you would do it like this:
    Code:
    Person ** persons = malloc(sizeof *persons * TOTAL);
    ...
    persons[0] = malloc(sizeof **persons);
    persons[0]->blah = blah...
    Note how you now have to allocate space for your array of pointers AND each pointer has to have space allocated for it to store the actual Person object it would then be pointing to. This is a different technique and is more suited to a "sparse" data structure where you don't expect all or most of the slots in the array to be filled at any given time. It's also much more difficult to free because you have to free each pointer first, then the whole array of pointers.

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    12
    You're right. However I didn't realize that '->' derrefers the pointer, so I should have access this way:

    persons->name
    ...
    (persons+1)->name
    ...

    whereas using the array notation, I'm getting the pointer value, so I would do

    persons[0].name
    ...
    persons[1].name
    ...

    as you said.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Gaspar Castillo View Post
    Code:
    fflush(stdin);
    Where did you learn this? Using this line of code results in undefined behavior. There is no valid way to "flush" an input stream in C. You aren't even accepting any input from the user in your program, so why do you need to bother with the standard input stream?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Feb 2015
    Posts
    12
    Quote Originally Posted by Elkvis View Post
    Where did you learn this? Using this line of code results in undefined behavior. There is no valid way to "flush" an input stream in C. You aren't even accepting any input from the user in your program, so why do you need to bother with the standard input stream?
    I know it makes a lot of noise! You HATE that kind of things. But I've read that this way I flush the stdin buffer, because when reading with scanf() and hit enter, ''\n" is not considered and stays in buffer. So for example, if I want to prevent my executable from exiting I use getchar(), and if "\n" stays on buffer, getchar() takes it as input and my program exits.

    Does it make sense?
    What do you think I should do about that?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    because when reading with scanf() and hit enter, ''\n" is not considered and stays in buffer.
    Yes the new line character will remain in the input buffer after a call to scanf(). However since your program doesn't contain any calls to scanf() you shouldn't need to worry about flushing the input buffer.

    The problem is that according to the C standard fflush() is only defined to work with output streams. Using it on input streams produces undefined behavior. So you should never use fflush() on an input stream.

    As far as keeping your console window open when the program terminates there are other, better ways of accomplishing this task. One way, since you appear to be using Visual Studio is to run the program in debug mode which will automatically keep the window open. Another way would be to actually run this console program in a cmd window instead of running it through the IDE.

    Jim

  9. #9
    Registered User
    Join Date
    Feb 2015
    Posts
    12
    Quote Originally Posted by jimblumberg View Post
    Yes the new line character will remain in the input buffer after a call to scanf(). However since your program doesn't contain any calls to scanf() you shouldn't need to worry about flushing the input buffer.

    The problem is that according to the C standard fflush() is only defined to work with output streams. Using it on input streams produces undefined behavior. So you should never use fflush() on an input stream.

    As far as keeping your console window open when the program terminates there are other, better ways of accomplishing this task. One way, since you appear to be using Visual Studio is to run the program in debug mode which will automatically keep the window open. Another way would be to actually run this console program in a cmd window instead of running it through the IDE.

    Jim
    Ok, I had commented part of my code above in which I used scanf().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array struct
    By trep in forum C Programming
    Replies: 10
    Last Post: 05-23-2012, 10:20 AM
  2. Please help with struct and dynamic array problem
    By stephenb in forum C Programming
    Replies: 5
    Last Post: 10-19-2011, 06:02 AM
  3. dynamic array of struct within dynamic array of struct
    By explodecomputer in forum C Programming
    Replies: 3
    Last Post: 08-03-2010, 03:25 AM
  4. dynamic array in STRUCT
    By cfdprogrammer in forum C Programming
    Replies: 15
    Last Post: 08-04-2009, 09:57 AM
  5. dynamic array of base class pointers
    By Corrington_j in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2003, 05:58 AM