Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

C: need help with pointers

MyName13
 Share

Go to solution Solved by Unimportant,

First of all, it won't compile, the call to the 'f' function is wrong. You need to pass the address of the array but you try to take the address of the type.

int main()
{
	const int n = 5;	

	SomeStruct *theArray = NULL; 
   	f(&theArray, n); //Pass the address of theArray instance, not the type SomeStruct. 
}

 

(re)alloc takes the amount of memory to allocate in bytes. Your struct probably consists of more then a single byte, so you need to allocate n times the size of the struct bytes:

*dynArray = (SomeStruct *)realloc(*dynArray, sizeof(SomeStruct) * n);

Additionally, you should test if the memory allocation actually succeeded before attempting to use the memory. (re)alloc returns a NULL pointer on failure.

 

As for how to access the struct member, there's no really clean way. The canonical way would be:

(*dynArray)[n - 1].someMember
  
//(*dynArray) dereferences the pointer to access the underlying array pointer
//[n - 1] accesses the last element
  
  
//For scanf you need to take the address of the member so it becomes:
&((*dynArray)[n - 1].someMember)

If you need to access a lot of members this way it becomes ugly. One could create a intermediate pointer to the array element to access as a proxy to make things cleaner:

SomeStruct *proxy = &((*dynArray)[n-1]); //proxy now points to array member n - 1.

scanf("%d", &proxy->someMember);
printf("%d", proxy->someMember); //Cleaner and more readable for multiple accesses.    
        

 

I need to pass a dynamic array of structures to a function that resizes that dynamic array.This is what I've done:

 

typedef struct strct
{
    ...
}SomeStruct;

void f(SomeStruct **dynArray, int n)
{	
	*dynArray = (SomeStruct *)realloc(*dynArray, n);
        //left side - I'm dereferencing dynArray so I can allocate the new block of memory to theArray
    	//right side - dereferencing dynArray because I need to reallocate the array (which is on the address dynArray is holding)
    
    
    
    
    scanf("%d", &(*((*dynArray)+n-1)).someMember);
    	//changing some int member of the newest structure, first I dereference dynArray,
      		//then I add n-1 (to get the last item of the array)
      			//then I dereference that to get access to the structure
        
        
    
}

int main()
{
	SomeStruct *theArray = NULL; //intialize an array
    f(&SomeStruct, n); //n is some size
}

This isn't working for some reason and the line in which I modify a struct member is very unreadable.What have I done wrong and how can I make this more readable?

Link to comment
Share on other sites

Link to post
Share on other sites

First of all, it won't compile, the call to the 'f' function is wrong. You need to pass the address of the array but you try to take the address of the type.

int main()
{
	const int n = 5;	

	SomeStruct *theArray = NULL; 
   	f(&theArray, n); //Pass the address of theArray instance, not the type SomeStruct. 
}

 

(re)alloc takes the amount of memory to allocate in bytes. Your struct probably consists of more then a single byte, so you need to allocate n times the size of the struct bytes:

*dynArray = (SomeStruct *)realloc(*dynArray, sizeof(SomeStruct) * n);

Additionally, you should test if the memory allocation actually succeeded before attempting to use the memory. (re)alloc returns a NULL pointer on failure.

 

As for how to access the struct member, there's no really clean way. The canonical way would be:

(*dynArray)[n - 1].someMember
  
//(*dynArray) dereferences the pointer to access the underlying array pointer
//[n - 1] accesses the last element
  
  
//For scanf you need to take the address of the member so it becomes:
&((*dynArray)[n - 1].someMember)

If you need to access a lot of members this way it becomes ugly. One could create a intermediate pointer to the array element to access as a proxy to make things cleaner:

SomeStruct *proxy = &((*dynArray)[n-1]); //proxy now points to array member n - 1.

scanf("%d", &proxy->someMember);
printf("%d", proxy->someMember); //Cleaner and more readable for multiple accesses.    
        

 

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Unimportant said:

First of all, it won't compile, the call to the 'f' function is wrong. You need to pass the address of the array but you try to take the address of the type.

 

(re)alloc takes the amount of memory to allocate in bytes. Your struct probably consists of more then a single byte, so you need to allocate n times the size of the struct bytes:


*dynArray = (SomeStruct *)realloc(*dynArray, sizeof(SomeStruct) * n);

 

 

1)I retyped the program wrong

2)it seems I have confused it with calloc, I haven't asked myself how the compiler will know the structure's size since it hasn't been specified anywhere.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×