Segmentation fault in C

i have this code

int already_there(char *client_names[], char *username) {
    int i;
    for(i = 0; i<NUM; i++) {
        printf("HERE\n");
        if (strcmp(client_names, username)==0) return(1);
    }
    return(0);
}

and i get a segmentation fault, whats wrong here?

Well, assuming:

  1. NUM is the length of client_names
  2. all the client names entries are not null, pointers to valid addresses,
  3. every client name is null terminated,
  4. so is username (a valid pointer to a null terminated string)

then, nothing. Maybe test client_names [i]for null. Incidentally, argv is such an array of pointers to char arrays, and while main() provides argc, it is really derived by an extra argv entry at the end, a null pointer, as described in man 2 exec(). Sometimes, the null term is more reliable, like if NUM changes, as there is no Num needed. Stick in some fprintf(stderr,"%d: . . . ", __LINE__, . . .); lines and log what works, unbuffered.

nvm i fixed it all i had to do is

if (client_names[i]== username) return(1);

Humm, you are not actually comparing strings here, all you are doing is comparing pointers. Not very robust.

Strings don't work that way. It's not crashing anymore, but it's not actually doing what you wanted either. It's just comparing the pointer, not the contents. Try this:

int main(void)
{
    char *str1="aaaa";
    char str2[]="aaaa";
    char *str3=str1;
    if(str1 == str2) printf("str1 == str2\n");
    if(str1 == str3) printf("str1 == str3\n");
}

It won't print the first one because str1 and str2 aren't equal. They point to different areas of memory. It will print the second one because str1 and str3 do point to the same area of memory.

As is often the case it's not something wrong with your function as much as something wrong with the data you fed into it. Something's wrong with the variables you passed into the function. Since you, as usual, didn't post the complete code, I'd need psychic powers to help you.

[i]Please post your complete code!

i defined client_names like
char *client_names[NUM];
and only one element was defined
how do i set the ones i am not using to not null ?

Do you not understand the meaning of "complete code"? I hope that's enough. :wall:

To set things to NULL, set them equal to NULL.

// Starting at one because zero has something in it
for(n=1; n<NUM; n++) { client_names[n]=NULL; }

...but that's not enough to stop crashes. NULLs aren't a warning for the computer -- they're a warning for you. The program must check if things are NULL before you use them.

int already_there(char *client_names[], char *username) {
    int i;
    for(i = 0; i<NUM; i++) {
        printf("HERE\n");
        // strcmp on NULL will crash
        if(client_names != NULL)
        {
            if (strcmp(client_names, username)==0) return(1);
        }
    }
    return(0);
}

If the array has one entry, then either NUM must be 1, or you must devise a way to handle missing names, like setting and testing null pointer values in the fixed size array.