Aman Deep Gautam
Aman Deep Gautam

Reputation: 8747

gcc warning: assignment makes integer from pointer without a cast

I am making this assignment in my program and I am getting the warning a titled. Here is the code snippet:

table_name[index] = NULL;

I could not understand what can be a problem in such a statement.

Upvotes: 2

Views: 4142

Answers (3)

When i try to assign a pointer value into a varriable (always the same data type) then I have the same reaction from the compiler..

example:

#include <stdio.h>
#include <stdlib.h>

int main(){

    int X,Y;
    int *P_1;
    float Z;
    float *P_2;

    X=5;
    Z=3.5;
    P_1=&X;
    printf("\n X = %d, Z = %.2f\n",X,Z);
    printf(" &X = %d\n",&X);
    printf(" *P_1 = %d\n",*P_1);
    printf(" P_1 = %d\n",P_1);  

    Y=X;
    printf(" Y = %d\n",Y);

    X=10;
    Y=P_1;
    fflush(stdin);
    getchar();
    return 0;   
}

Upvotes: 0

Levon
Levon

Reputation: 143022

NULL is not a valid integer, and it is being assigned to an entry of an array made up of presumably ints, so the compiler is complaining.

NULL is used as a default pointer value that indicates "nothing" .. if you had a pointer variable and assigned NULL to it, you'd be saying that pointer variable points to "nothing".

Because of this type mismatch, the specific message is warning you that you are trying to assign one type (really a pointer value) to another (int) without trying to cast it (which is how we sometimes convert one type to another in order to avoid type mismatches).

If you had an array of pointers this assignment of NULL would be perfectly ok.

Upvotes: 6

Reed Copsey
Reed Copsey

Reputation: 564413

The problem is that table_name[index] is an integer value, not a pointer. NULL is meant to represent pointers.

If table_name is declared as int*, you could do table_name = NULL; without issue, but when setting a value at an index, the compiler is going to expect you to use the same type as the array.

Upvotes: 1

Related Questions