1

i am kind of new in c and i am trying to figure things out.

my question is, what i need to have in the place of '/0' , in order for it to skip the "empty" cells?

i know i could do it the easy way and just have have all the .anoxi values in the condition, but i was just curious. i have tried putting "", which gives me all the names (doesn't skip any of them) , '' which gives me "[Error] empty character constant" and null, which gives me "[Error] 'null' was not declared in this scope"

struct t { 
    char anoxi[10];
    char name[10];
    char gramma [2];
}

int main() {
    struct t array[5][12];
    int r;
    strcpy(array[4][1].anoxi, "+-1%");
    strcpy(array[4][2].anoxi, "+-2%");
    strcpy(array[4][5].anoxi, "+-0.5%");
    strcpy(array[4][6].anoxi, "+-0.25%");
    strcpy(array[4][7].anoxi, "+-1%");
    strcpy(array[4][8].anoxi, "+-0.05%");

    strcpy(array[0][0].gramma, "M");
    strcpy(array[0][1].gramma, "K");
    strcpy(array[0][2].gramma, "N");
    strcpy(array[0][3].gramma, "O");
    strcpy(array[0][4].gramma, "I");
    strcpy(array[0][5].gramma, "R");
    strcpy(array[0][6].gramma, "L");
    strcpy(array[0][7].gramma, "V");
    strcpy(array[0][8].gramma, "G");
    strcpy(array[0][9].gramma, "A");
    strcpy(array[0][10].gramma, "X");
    strcpy(array[0][11].gramma, "S");

    strcpy(array[1][0].name, "Black");
    strcpy(array[1][1].name, "Brown");
    strcpy(array[1][2].name, "Red");
    strcpy(array[1][3].name, "Orange");
    strcpy(array[1][4].name, "Yellow");
    strcpy(array[1][5].name, "Green");
    strcpy(array[1][6].name, "Blue");
    strcpy(array[1][7].name, "Purple");
    strcpy(array[1][8].name, "Grey");
    strcpy(array[1][9].name, "White");
    strcpy(array[1][10].name, "Gold");
    strcpy(array[1][11].name, "Silver");

    for (r=0; r<12; r++) {
        if (array[4][r].anoxi!= '\0') {
            printf("%s = %s\n",array[0][r].gramma, array[1][r].name);
        }
    }
    return(0);
}
3
  • 2
    Μπάμπη, please do me a favor, and indent this code. Welcome to SO!
    – gsamaras
    May 17, 2016 at 20:07
  • Yes Xaralabos, @AnT is right. You should be really careful when it comes to tagging. I re-tagged your question to c. Now that you learnt it, I am sure you won't make it again. :)
    – gsamaras
    May 17, 2016 at 21:05
  • 1
    my bad, ima try to double check it from now on May 17, 2016 at 21:10

3 Answers 3

5

C has no concept of "empty". Variables in C represent actual physical memory locations, and they contain whatever that memory contains, which is either what they were initialized to contain, or some random value if they were never initialized (note that statics are initialized by default).

This doesn't prevent you from choosing to interpret one of the possible values of a variable as "empty", but that would be your choice, and entirely up to you. You would then have to initialize your variable/array with that value and check for it. Character variables often use the value '\0' for this, which should work for you--just make sure you take care of the difference between single characters and arrays: for example, ... if...gramma[0] == '\0'' ...

There is what you might consider an exception to this: one of the values pointer variables are allowed to take is a value called NULL, which is guaranteed not to point to anything. This is often used to initialize pointer variables but you still have to do the initialization and checking yourself.

1
  • 1
    You should probably mention that static variables do get default inialization, but automatic variables don't.
    – Barmar
    May 17, 2016 at 20:11
3

Now that you got, it you may want to read my Structs (C), which offers a compact example, that might come in handy*.


First of all, allow me to question the validity of this code. Consider this equivalent example I made:

#include <stdio.h>

struct t { 
    char anoxi[10];
    char name[10];
    char gramma [2];
};

int main(void) {
        struct t my_array[5][1];
        if(my_array[0][0].anoxi != '\0')
                printf("%s\n", my_array[0][0].anoxi);
        return 0;
}

It will print, in my machine:

gsamaras@gsamaras-A15:~$ gcc -Wall px.c
gsamaras@gsamaras-A15:~$ ./a.out 
����

Why?

Because, the memory the array holds is not initialized to anything, so its value is undefined, which invokes UNDEFINED BEHAVIOR!

We could fix this, by initializing every string, like this:

    struct t my_array[5][1];
    my_array[0][0].anoxi[0] = '\0';
    if(my_array[0][0].anoxi[0] != '\0')
            printf("edw %s\n", my_array[0][0].anoxi);

Or, as Mike suggested, you could use memset(), like this:

memset (my_array, 0, sizeof (my_array);

Usually we set the value of a variable to a predefined value, which for us, humans tells that this cell/string/whatever is empty.

does not know that, unless we tell our program to keep an eye out of empty "things". We have to inform our program what is an empty "thing", especially how to identify it!

Here, you have a string and you check array[4][r].anoxi!= '\0', which is always true because the left-hand side is an array, which decays to pointer in this expression, as M.M said.


*I am not writing it here, since the answer is already too long

12
  • i tried printing them but each cell have a different value, like, printf("%s\n",pinakas[4][0]); prints ! , while pinakas[4][3] prints @.....i can also print them without getting error as just chars and as integers, thats why i am so confused May 17, 2016 at 20:20
  • Yes exactly, you see, they print random stuff, because the memory is not initialized. When I do char my_array[5], the system allocates 5 contiguous memory positions, every one of size of char. I do not know what this memory has, thus when I print, I do not know what I am going to see. However, if I also do strcpy(my_array, "hello babis"); and then print the string, I know what I am going to see, since I filled the array with something, it is not uninitialized now! @XaralabosNikolaidhs now that I now you got, it I would like you to check my last edit, it might help you.
    – gsamaras
    May 17, 2016 at 20:51
  • ok, so i tried adding values to the "empty" cells (strcpy(pinakas[4][3].anoxi, "Empty"); and so on, and i tried - if (pinakas[4][r].anoxi!="Empty")- and it still show all 12 names May 17, 2016 at 20:56
  • @XaralabosNikolaidhs, use strcmp to do that, like if(strcmp(pinakas[4][r].anoxi, "Empty") != 0) and tell how it goes.
    – gsamaras
    May 17, 2016 at 20:59
  • 1
    WOW, IT WORKED, tnx a lot May 17, 2016 at 21:03
1

First of all, initialize the array to blank:

struct t array[5][12] = { 0 };

This means that any members you have not yet assigned contents to will have value 0 (converted to the type of that member). This is so that later on you can see if the member has been assigned something else by checking to see if it is still 0 or not.

Then you can check:

if ( array[4][r].anoxi[0] ) {
//                   ^^^^

Note that you must check anoxi[0] which is a char object. Checking anoxi, which is an array object, merely checks that the array exists in memory (which it tautologically does), not whether the contents of the array are some particular value.


NB. The != '\0' is redundant, I think it is clearer to omit it but you could use it if you want.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.