0
typedef struct test
{
  int numInt;
  double numFloat;
  bool check;
} TEST;

int main ()
{
    int j=0;
    TEST *myNum[20];  
    // I just want an array to hold 20 addresses of TEST structs.

    TEST tempTESTS[20];  // Lets create 20 structs as an example.

    for (int i=0; i<20; i++)
    {
        if (tempTESTS[i].numInt > 100)
        {  
           myNum[j] = &tempTESTS[i];  
           ++j;
        }
    }

    print (myNum, j);
}

Edit: I want to store the addresses of the structs that pass certain condition in the "myNum array", and then pass that array to another function to process them (print for example).

Isn't this correct?

For some reason, I'm confused if I should malloc each element of (*myNUM[20]) array before storing the address of "tempTESTS struct". I just want to store the address of a valid struct here. So, I dont think malloc is needed. Could someone clarify? I wish I could chat with a person to clear this :-(

Thanks!

test123
  • 1
  • 1
  • 3
    Refer to this [question](https://stackoverflow.com/questions/8800482/when-and-why-to-use-malloc) for how and when to use malloc to clear things up. Using malloc in your case depends on what you're trying to achieve. – Wahalez Jul 29 '21 at 08:03
  • I will refer to your edit. You might still not want to use malloc. If the struct doesn't pass a certain condition, store some address like 0. In the function that needs to process the references to the struct array, check if the reference is of value 0, if it does just skip to the next line with [continue](https://www.programiz.com/c-programming/c-break-continue-statement) – Wahalez Jul 29 '21 at 08:18
  • 2
    `malloc` creates an object. Decide whether you want a new object, or whether you want to refer to an existing object . – M.M Jul 29 '21 at 08:19
  • If you just want to store the addresses of the structs that pass a certin conditiom, your code is just fine. – Jabberwocky Jul 29 '21 at 08:21
  • It does depend on what print does. If it is in the same thread, then it should work. If it is in a different thread, then, because the threads can't see each others' stacks, it has to be malloced. – cup Jul 29 '21 at 08:24
  • Thanks guys! I want to use the existing objects (by storing their address in that array). I dont want to create a new copy. So, I dont need to use malloc - right? I got completely confused, and started doubting everything! – test123 Jul 29 '21 at 08:25
  • @cup: Same thread. As main () is still running when "print" is called, the stored addresses in the array (in stack) should be still good. – test123 Jul 29 '21 at 08:30
  • @test123 malloc is definitely not needed in that case. With malloc you'd create a duplicate for each copied struct. – Jabberwocky Jul 29 '21 at 08:30
  • @test123 other threads _can_ see each others' stack, no problem with that. But of course if a local variable goes out of scope, then another thread cannot use it any more. – Jabberwocky Jul 29 '21 at 08:33
  • The test of `tempTESTS[i].numInt` in the loop doesn't make sense, since those structures, and all of their fields, are completely undefined when that code executes. So the test will exhibit undefined behavior, and in general will depend on what values happen to be on the stack when `main` is called. Even if you get lucky, and they happen to all be zero, then the test would always fail so the loop would serve no purpose. – Tom Karzes Jul 29 '21 at 08:37

0 Answers0