1

I was hoping someone could clear this up for me, the searches seem to be ignoring the bang.

If I put

while(!NULL)
{
    // do this
}

would that be equivalent to saying continue doing this while the value is NULL? or does this mean continue doing this while not equal to NULL?

I've also been reading up on structs but it doesn't seem to be clicking. Given the example:

typedef struct node
{
    int frog;
    int toad;
} node;

What does each utterance of 'node' accomplish? how to they effect the structure if the other isn't present?

I heard something about the last one creating a variable of type node but this doesn't entirely make sense (here's the link to the description that made the most sense to me http://www.programiz.com/c-programming/c-structures)

1

Whatever you put inside the parenthesis is considered/has to be a condition. So it could be while (a == 5) or if a variable is itself a condition you can put the variable directly in the parenthesis. For example every integer, except for 0, is considered as true, so if I have while (10) this condition will always be true (and will probably cause an infinite loop).

Now the value NULL is interpreted as false. The ! just changes the condition to its opposite. So true becomes false and false becomes true. Take a look at the example below:

#include <stdio.h>

int main(void)
{
    if (NULL)
    {
        printf("NULL is interpreted as 'true'\n");
    }
    else if (!NULL)
    {
        printf("NULL is interpreted as 'false'\n");
    }

    return 0;
}

It will print

NULL is interpreted as 'false'

Now on structs. You basically create a struct whenever you want a type for a variable, that holds more than one type of variables. For example if we had a store with PCs and wanted to store them somehow, you could create a struct that would contain each PC's variables (i.e. CPU speed, RAM etc). The struct would look like that:

struct pc
{
    float cpu_frq;
    int gb_of_ram;
};

Now every time you wanted to create a variable of this type inside your program you would have to do it that way:

struct pc pc_name;
pc_name.cpu_frq = 3.2;
pc_name.gb_of_ram = 8;

In order to not have to type the whole struct name (i.e. struct pc) we use a typedef, which creates an 'alias' for a variable type. So you can change to

typedef struct pc {
    float cpu_frq;
    int gb_of_ram;
} apc;

and declare your variables as

apc pc_name;
pc_name.cpu_frq = 3.2;
pc_name.gb_of_ram = 8;

So to answer directly your question the top node is the name of the struct and the bottom node is the 'alias' name of the struct.


If you didn't use the typedef at top, what is now the 'alias', would be declaration of variables. So that

struct pc {
    float cpu_speed;
    int gb_of_ram;
} apc;

would have this as consequence:

apc.cpu_speed = 3.2;
apc.gb_of_ram = 8;

If something is unclear (probably many things as I didn't use a really great example) ask away!

  • Have I understood correctly, that struct pc { ... } apc; will create single variable apc of type struct pc in that declaration's scope? – Wiktor Czajkowski Jul 28 '15 at 21:24
  • Yes you are correct. – ChrisG Jul 28 '15 at 21:26
  • Just to be clear, bool misspelled = !check(word), if check runs and returns false, the bool variable misspelled would == true? and do you need the alias when using typedef? Thank you very much for your explanations. – Gibb Johnson Jul 29 '15 at 17:44
  • Yes you are correct. – ChrisG Jul 29 '15 at 17:48
  • Just a note since you learned about typedef. In C there isn't actually the type bool. It's a typedef of int. And true is 1 and false is 0. – ChrisG Jul 29 '15 at 17:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .