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

Kroganite
  • 5
  • 3
  • You're right. What's the problem? – michaelmeyer May 03 '14 at 20:19
  • When I call the function it only displays one result instead of "num" results. – Kroganite May 03 '14 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. – Crowman May 03 '14 at 20:27
  • Do you want 1 or multiple `struct plane` ? Also, [do not cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Chnossos May 03 '14 at 20:30
  • @user3599935: Nowhere in your code you make any attempts to "display" anything. How is it possible it "displays" something? – AnT stands with Russia May 03 '14 at 20:34
  • @user3599935 There's no code here that displays anything, so it's rather hard for us to guess what's happening on your computer. The function here looks ok, so your problem is something else. So, describe what you do , and what happens. And what you expect to happen. – nos May 03 '14 at 20:35
  • Basically this is an already created program with function skeletons. I'm only creating the functions to make the program run right. This is the first function to create a link list. When I run the code I have above, and call the already created CORRECT FUNCTION TO SORT (was given to me by teacher) in main, it only displays one plane...not num amounts and it should display multiple planes info – Kroganite May 03 '14 at 21:09

1 Answers1

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
}
edgar.holleis
  • 4,803
  • 2
  • 23
  • 27
  • 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 – Kroganite May 03 '14 at 20:22