struct pointers and functions

This is a discussion on struct pointers and functions within the C Programming forums, part of the General Programming Boards category; Hello everyone I want a struct in which one member is a pointer (arrProb) and the other is an integer ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    8

    struct pointers and functions

    Hello everyone

    I want a struct in which one member is a pointer (arrProb) and the other is an integer to save the quantity of elements in the array.

    Code:
    typedef struct MODELOBJECT{
      
      int numObjects;
      double *arrProb; // TODO: Se tiene que hacer un realloc cada vez que se almacenar'a otro valor.
    
    } ModelObject;
    I make a malloc to define the size of data to store, but as you guys can see

    And these are my problems
    1. (See red line) How to allocate memory for the arrProb (although no apparent problems)

    2. I have a compilation error in the function modelObject_printArrProb(ModelObject *modelObject)
    Code:
    $ gcc borrador1.c -o borrador1
    borrador1.c: In function ‘modelObject_printArrProb’:
    borrador1.c:11: error: request for member ‘numObjects’ in something not a structure or union
    borrador1.c:13: error: request for member ‘numObjects’ in something not a structure or union
    borrador1.c:14: error: request for member ‘arrProb’ in something not a structure or union
    I want pass the structure as a pointer to another functions.

    I hope guys can help me.

    These is the complete code.
    Code:
    #include <stdio.h>
    #include <malloc.h>
    typedef struct MODELOBJECT{
      
      int numObjects;
      double *arrProb; // TODO: Se tiene que hacer un realloc cada vez que se almacenar'a otro valor.
    
    } ModelObject;
    
    void modelObject_printArrProb(ModelObject *modelObject){
    	printf("modelObject.numObjects = %d",modelObject.numObjects);
    	int i;
    	for(i=0;i<modelObject.numObjects;i++){
    		printf("arrProb[%d] = %f\n",i,modelObject.arrProb[i]);
    	}
    
    }
    
    
    int main(){
    
    	ModelObject modeloObjetos;
    	modeloObjetos.numObjects=5;
    	printf("sizeof %d\n",sizeof(modeloObjetos));
    	printf("modeloObjetos.numObjects = %d\n",modeloObjetos.numObjects);
    	printf("modeloObjetos.arrProb = %u\n",modeloObjetos.arrProb);
    	printf("sizeof modeloObjetos.arrProb %d\n",sizeof(modeloObjetos.arrProb));
    	modeloObjetos.arrProb = (double*)malloc(modeloObjetos.numObjects*sizeof(double *));
    	printf("after malloc\n");
    	printf("sizeof modeloObjetos.arrProb %d\n",sizeof(modeloObjetos.arrProb));
    	
    	printf("modeloObjetos.arrProb = %u\n",modeloObjetos.arrProb);
    	int i;
    	int tope = modeloObjetos.numObjects;
    	tope = 10;
    	for(i=0;i<tope;i++){
    		modeloObjetos.arrProb[i]=rand();
    		printf("modelo.arrProb[%d] = %f\n",i,modeloObjetos.arrProb[i]);
    	}
    	printf("=======================================\n");
    	printf("sizeof modeloObjetos.arrProb %d\n",sizeof(modeloObjetos.arrProb));
    	printf("modeloObjetos.arrProb = %u\n",modeloObjetos.arrProb);
    //	modelObject_printArrProb(&modeloObjetos);
    
    	return 0;
    }
    Best Regards

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You use -> to access elements of structures that are pointed to.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> printf("modelObject.numObjects = %d",modelObject.numObjects);

    The variable 'modelObject' is a pointer, of course, which uses a different syntax for accessing members ('->', as opposed to '.'), eg:

    printf("modelObject.numObjects = %d",modelObject->numObjects);

    >> sizeof(modeloObjetos.arrProb)

    This will always be sizeof(double*). The data that it points to doesn't affect this.

    >> modeloObjetos.arrProb = (double*)malloc(modeloObjetos.numObjects*sizeof(do uble *));

    Careful. Since you are allocating an array of double (not double*), the sizeof expression should be of type double.

    Finally, don't forget to 'free' what you malloc.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    ps. they call this (->) indirect notation.
    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
    Aug 2009
    Posts
    8
    thanks people that works really well. But i don't understand why because earlier i proof (.*) instead (->) and don't works either.

    And how should make the memory allocation for my variable *arrProb whit other examples if i not use a malloc the program generate segmentation fault. but in this program not!

    Thats the reason why i put this in the program

    Code:
    modeloObjetos.numObjects=5;
    	modeloObjetos.arrProb = (double)malloc(modeloObjetos.numObjects*sizeof(double));
    
    	int i;
    	int tope = modeloObjetos.numObjects;
    	tope = 10;
    	for(i=0;i<tope;i++){
    		modeloObjetos.arrProb[i]=rand();
    		printf("modelo.arrProb[%d] = %f\n",i,modeloObjetos.arrProb[i]);
    	}
    This red line must generate an error. But nothing happen!!
    Thanks again

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by alfaceor View Post
    thanks people that works really well. But i don't understand why because earlier i proof (.*) instead (->) and don't works either.
    Not sure what you are trying to imply here.
    Quote Originally Posted by alfaceor View Post
    And how should make the memory allocation for my variable *arrProb whit other examples if i not use a malloc the program generate segmentation fault. but in this program not!
    You need malloc() to dynamically store the array of doubles that the pointer arrProb will be pointing at altho' I am still not sure what you're trying to say.
    Quote Originally Posted by alfaceor View Post
    Thats the reason why i put this in the program

    Code:
    modeloObjetos.numObjects=5;
    	modeloObjetos.arrProb = (double)malloc(modeloObjetos.numObjects*sizeof(double));
    
    	int i;
    	int tope = modeloObjetos.numObjects;
    	tope = 10;
    	for(i=0;i<tope;i++){
    		modeloObjetos.arrProb[i]=rand();
    		printf("modelo.arrProb[%d] = %f\n",i,modeloObjetos.arrProb[i]);
    	}
    This red line must generate an error. But nothing happen!!
    Thanks again
    It doesn't generate an error because modeloObjetos is an object of type struct not a pointer to one and modeloObjetos.numObjects is the correct way of accessing a member of a struct object. If it were a pointer then you would use the right-hand arrow notation instead of a dot ie modeloObjetos->numObjects.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> But i don't understand why because earlier i proof (.*) instead (->) and don't works either.

    You can dereference the pointer, and then use normal structure access notation, eg:

    printf("modelObject.numObjects = %d", (*modelObject).numObjects);

    >> tope = 10;

    That specific line won't generate an error, but it does introduce a bug, in that you may later overstep your array. The program may not crash, but this is only *pure* luck. It is nontheless incorrect.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    Quote Originally Posted by Sebastiani View Post
    That specific line won't generate an error, but it does introduce a bug, in that you may later overstep your array. The program may not crash, but this is only *pure* luck. It is nontheless incorrect.
    I assume the malloc() is incorrect. So how can correct this bug?

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> I assume the malloc() is incorrect. So how can correct this bug?

    Just as you have it now, eg:

    Code:
    modeloObjetos.numObjects=5;
    modeloObjetos.arrProb = malloc(modeloObjetos.numObjects*sizeof(double));
    The 'bug' I was referring to was the fact that you set the loop limit to 'tope', which may or may not be the actual length of the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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