Incompatible types in assignment. C

421 views Asked by At

This part of my code:

    char MAC_ADRESSES[MAX_LINES][100];
    for(j=i+1; j<=countlines; j++)
        {
            if((MAC_ADRESSES[j])==(MAC_ADRESSES[i]))
            {
                MAC_ADRESSES[j] = NULL;
            }

At the point where I want to change the string with a NULL I have a compiler error about incompatible types assignment. Do not understand why..

5

There are 5 answers

0
Carl Norum On BEST ANSWER

Presumably MAC_ADRESSES is not an array of pointers. NULL is a pointer (normally (void *)0 in C), so you can't assign it to a non-pointer variable.

Edit: Since your definition is char MAC_ADRESSES[MAX_LINES][100], you have a 2D array, not an array of pointers. You can't store NULL in this array. You can wipe out a string by putting a null character in the first byte, though:

            MAC_ADRESSES[j][0] = '\0';

Note that you can't test strings for equality using ==, either. You should be using strcmp.

2
P0W On

Instead of NULL use '\0'

NULL is macros defined as

#define NULL (void *)0

or

#define NULL 0 , hence the

warning: assignment makes integer from pointer without a cast

As per your edited question use:

MAC_ADRESSES[j][0] = '\0';

0
Farouq Jouti On

if MAC_ADRESSES isn't a pointer array then MAC_ADRESSES[j] = NULL; is wrong because NULL is a pointer and MAC_ADRESSES is not

0
Ed Heal On

I think you need this

for(j=i+1; j<=countlines; j++)
    {
        if(strcmp(MAC_ADRESSES[j],MAC_ADRESSES[i]) == 0)
        {
            MAC_ADRESSES[j][0] = 0;
        }
    }

Given that

char MAC_ADRESSES[MAX_LINES][100]

As I guess you are trying to remove duplicates

BTW MAC_ADDRESS[j] is an array - not a pointer! Hence == operator will not make any sense

0
Arunprasad Rajkumar On

NULL is defined something like below

#define NULL (void*)0

So you can't assign void* type to char[][].(both are incompatible types)

If you want to invalidate use '\0' or 0;

MAC_ADRESSES[j][0] = '\0';

      (or)

MAC_ADRESSES[j][0] = 0;

(Note: Also you should use strcmp / stricmp / strcasecmp to compare the c string & == is not allowed on array bases)