Dynamically Create and Populate and Array in C


I'm a C beginner and I'm trying to use a pointer to an array of sender_id_t to populate a new array.  The goal is to have the getAcknowledgements function return a new array of sender_id_t id values.  I have the below implementation that I know is not right as I get a "assignment makes integer from pointer without a cast" exception.  The problem seems to lie in the foo[n]=ids->id; line of code as I'm not sure what I need to do to cast it properly.  The sender_id_t's id values are something like "ERT123GDSZX-22".  Is unsigned char* return valu appropriate?  Any help would be greatly appreciated!

struct sender_id_t_ {
    unsigned char id_v1_t[32];       id;
    uint32_t              phy_idx;

unsigned char * getAcknowledgements(resultset_t* resultset){
    int n;
   //returns an int of the number of rows for array
    int count = getAckCount(resultset); 
    unsigned char foo[count];
   //a pointer to an array of sender_id structures that I want to use to populate array
    sender_id_t *ids = getAcks(resultset); 
    for (n=0; n < count; n++){


Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi cgray1223,

first thing to mention is that you declared sender_id_t_::id without a type. That's ok in C (will default to int) but shouldn't be done anyway.

And next: Is it an exception (so a runtime error) or a compile error? Is the posted code exactly what you have tried? IMO this code can't compile because the statement unsigned char foo[count]; can only be used if count is known at compile time.

Another problem with the code is how the array is returned. Could you post the implementation of getAcks? If it's written similar to the posted function the problem may be that the data where ids points to isn't valid. In your code you fill a local instanced unsigned char foo, but this means the array is created on the stack when the function is entered and deleted from the stack when the function terminates, so the returned pointer doesn't point to valid data.

To allocate and return a pointer to an array you have to use a function which allocates the array in the heap, i.e. malloc like this
unsigned char* foo = (unsigned char*)malloc( count * sizeof( unsigned char ) );

Open in new window

When you return foo it now still points to valid data. But you have to take care that you release the data from the calling function when it's not needed anymore, i.e.:
unsigned char* foo = getAcknowledgements(...);
// do what you need to do with foo
free( foo );

You'll have to do it like this in any function which should allocate and return an array.

Hope that helps,

Your getAcknowledgements prototype is:
> unsigned char * getAcknowledgements(resultset_t* resultset);

But, you are saying that:
> getAcknowledgements function return a new array of sender_id_t id values

If, you want it to return sender_id_t, then you should change the return type.

Nos, in C, you cannot return an array.
If you really want to return the array, put that inside a structure and return the structure.

But, still your problem is not clear. If you can explain with some clear example, then we can try to help you further.
Few questions,

      What is your input ? ie what is in resultset_t struct ?
      what is the intent to return size of  a char buffer or something?
      It is not clear that what you have wanted to do with the sender_id_t and in the for loop may be it will  
      be solved simply if you explain the questions bit further?
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

cgray1223Author Commented:
Thanks for the responses.  The input for this question doesn't matter.  I just need to create a function that returns an array of sender_id_t_ structs.  In C I think the proper way is to return a pointer to an array of sender_id_t_ structs, correct?  I'm basically trying to create a method that just returns static data for now (thats why input doesn't matter).  I want to create, and populate an array of sender_id_t_ and then return whatever is appropriate in C.  The most important part is to get the function signature correct so that when I'm ready I will remove the static implementation and swap in a real implementation.

       I have posted an example code , which will help you to get what you have wanted
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct sender_id_t_ 
    unsigned char id_v1_t[32];      
	unsigned int id;
    unsigned int phy_idx;

sender_id_t *getAcknowledgements(void);
void printStructArray(sender_id_t *psender_id_t,int count);

int main(int argc,char *argv[])
	sender_id_t *psender_id_t = NULL;
	psender_id_t = getAcknowledgements();

sender_id_t *getAcknowledgements(void) /* I have n't given any input parameter here */
	int i;
	char *id_vi_t[10] = {"Test1","Test2","test3","Test4","Test5","test6","Test7","Test8","test9","Test10"};
	unsigned int ids[10] = {1,2,3,4,5,6,7,8,9,10};
	unsigned int phy_idx[10] = {1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};
	int count = 10;
	/* Dynamicallly create structure array of size 10 */
	sender_id_t *pSender_id_t = (sender_id_t *)malloc(count * sizeof(sender_id_t));
	for(i = 0; i < count; i++)
		pSender_id_t[i].id = ids[i];
		pSender_id_t[i].phy_idx = phy_idx[i];
	return pSender_id_t;

/* Going to print the array of sturctures ie implemented dynamically creating 10 arrays structures */
void printStructArray(sender_id_t *psender_id_t,int count)
	int i;
	for(i = 0; i < count; i++)
		printf("psender_id_t[%d].id_v1_t  = %s\n",i,psender_id_t[i].id_v1_t);
		printf("psender_id_t[%d].id           = %d\n",i,psender_id_t[i].id);
		printf("psender_id_t[%d].phy_idx = %d\n",i,psender_id_t[i].phy_idx);

Open in new window

And the output of the code is :
psender_id_t[0].id_v1_t  = Test1
psender_id_t[0].id           = 1
psender_id_t[0].phy_idx = 1001
psender_id_t[1].id_v1_t  = Test2
psender_id_t[1].id           = 2
psender_id_t[1].phy_idx = 1002
psender_id_t[2].id_v1_t  = test3
psender_id_t[2].id           = 3
psender_id_t[2].phy_idx = 1003
psender_id_t[3].id_v1_t  = Test4
psender_id_t[3].id           = 4
psender_id_t[3].phy_idx = 1004
psender_id_t[4].id_v1_t  = Test5
psender_id_t[4].id           = 5
psender_id_t[4].phy_idx = 1005
psender_id_t[5].id_v1_t  = test6
psender_id_t[5].id           = 6
psender_id_t[5].phy_idx = 1006
psender_id_t[6].id_v1_t  = Test7
psender_id_t[6].id           = 7
psender_id_t[6].phy_idx = 1007
psender_id_t[7].id_v1_t  = Test8
psender_id_t[7].id           = 8
psender_id_t[7].phy_idx = 1008
psender_id_t[8].id_v1_t  = test9
psender_id_t[8].id           = 9
psender_id_t[8].phy_idx = 1009
psender_id_t[9].id_v1_t  = Test10
psender_id_t[9].id           = 10
psender_id_t[9].phy_idx = 1010

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
And free the memory atlast,like
int main()
   sender_id_t *psender_id_t = NULL;
   psender_id_t = getAcknowledgements();

Open in new window

Hm - not nice that you completeley ignored my comment allthough it contains desription of all points which were wrong with the original posted code. Please write next time you just want working code but no explanations ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.