Can someone please explain this error and how to fix it? This worked perfectly on my computer but it stopped working when I transferred it to another computer.

int hash = 0; 
char *strings[100];
if((int)strings[i] != 0)
if((int) strings[hash] != 0)
while((int) strings[hash] != 0)
if((int)strings[hash] != 0)
if((int)strings[hash] != 0)

It is not at all clear what you are trying to achieve, however strings is an array of pointer to char (char *) so strings[i] (or any other index) has type char *, it is a pointer.

I have no idea why you would then cast to integer as testing a pointer against 0 is completely valid; check pointer is not NULL if(strings[i] != 0), it would be slightly more normal to use NULL if(strings[i] != NULL) (also note existance of nullptr symbol in C++11).

Casting a pointer to int is dangerous because you do not know that for any system the number of bits in an int is the same as the number of bits in a pointer and if they aren't you will (normall since if this is the case pointers are normally longer) truncate the pointer value.

And this is particular issue if you cast the output of malloc when not in the presence of a predeclaration of malloc since the compiler then assumes it returns int which is why you shouldn't cast the output of malloc in C.

I want to use strcmp on my array of string. I want to know if my value is already in my array. I obviously can't do that on my null values or I will have a segmentation fault. So since I know the ascii value of Null is 0 I was preventing it from reaching the strcmp with the cast.

I removed all of my int casts and I still get a segmentation fault. Should I also chang all my "0" to NULL?

for(i = 0; i < 50; i++)
{
    //printf(" i is %d \n", i);
    if(strings[i] != 0)
    {
        //printf(" Top if 1 \n");
        /*if(val == 0)
        {
            printf(" val is %d so breaking \n", val);
            break;
        }*/
        if(strcmp (strings[i], nam) == 0)
        {
            //printf(" strings[i] is equal\n");
            //printf(" strings[i] is %s \n", strings[i]);
            //printf(" nam is %s \n", nam);
            string_equal_flag = 1;
            break;
        }
        //printf(" Top if 2 \n");
    }
    if(i == 49)
    {
        string_not_flag = 1;
        //printf(" set string_not_flag\n"); 
    }
}

Before comparing a pointer to NULL you have to initialize them to null when you declare the pointers (or at least some time before using them). If you don't then the the value of those pointers are whatever happens to be in memory at the time they are declared, which may or may not be 0.

char *strings[100] = {0};

The above will set all 100 pointers to NULL (which is the same as 0)

Thank you :). It worked :).

If I would have used calloc would I of still had to initialize strings? Would something like that have worked? From what I have been reading about calloc it sounds like it would have done it for me.

strings[hash] = calloc(strlen(mneumonic)+1);

In that case calloc() is unnecessary. If all you want to do is make a copy of another string then just do this:

strings[hash] = strdup(mneumonic);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.