0

Okay so I'm currently learning C in Visual Studio 2010, and my goal is to allocate enough memory for a plane structure (containing air traffic controller simulation info) of "num" elements. Below is the code I have for that function. The setData is an already completed working function that stores individual plane struct data. MY QUESTION: how do I make it call setData "num" amount of times and return all the elements to the main? My thinking was loop the data to store per element put when I return its the first byte of allocated memory....which is element one instead of all of them.

struct plane* list_intialize(unsigned int num)
{
struct plane * ptr;
int i=0;

ptr = (struct plane *) malloc(num * sizeof (struct plane));

    for(i=0;i<num;++i)
        setData(&ptr[i]);

return ptr;
}

HERE IS MY INSTRUCTIONS FOR THIS FUNCTION: Here an unsigned integer listsize is passed to this function you are to create a link list of size listsize. This will be performed by repeated use of malloc and calling setData to initialize the data into the struct plane fields. Each time you place the process in the list you need to place it so the list is sorted by the field distance (in ascending order). you return the head of the list

I have the ascending order sort I just need the rest

7
  • You're right. What's the problem? Commented May 3, 2014 at 20:19
  • When I call the function it only displays one result instead of "num" results. Commented May 3, 2014 at 20:21
  • Post an entire, but as short as possible, working program. There are too many other things you could be doing wrong, here, and we need to see them. Commented May 3, 2014 at 20:27
  • Do you want 1 or multiple struct plane ? Also, do not cast the result of malloc. Commented May 3, 2014 at 20:30
  • @user3599935: Nowhere in your code you make any attempts to "display" anything. How is it possible it "displays" something? Commented May 3, 2014 at 20:34

1 Answer 1

1

You can rely on pointer arithmetic:

int i;
for (i = 0; i < num; ++i) 
    setData(ptr+i);

Or you can use array syntax:

int i;
for (i = 0; i < num; ++i) 
    setData(&ptr[i]);

Both are essentially the same.

The return value (ptr - pointer to first array element) stands for the whole array. You can access the whole field as follows:

// main
struct plane *pPlanes;
pPlanes = list_initialze(10);
int i;
for (i = 0; i < 10; ++i) {
    // do something with plane
    printf("%d\n", pPlanes[i].structelement);    // array syntax
    printf("%d\n", (pPlanes+i)->structelement);  // pointer arithmetic
}
Sign up to request clarification or add additional context in comments.

1 Comment

I actually used the exact setup of your second example but when calling the function in main I only get one structure element return instead of num amount

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.