Home > OS >  Comparing int and int* in C
Comparing int and int* in C

Time:12-25

I just started C and I'm somewhat confused. As an example:

    int *first=0;
    int second=1;
    if(*first<second)
    {
        printf("Works\n");
    }

The above example is supposed to give me the print and finish the code. Instead, it gives me a seg error, but the IDE tell me there are no problems. In first, aren't I giving the value the pointer is pointing at? What am I missing here?

CodePudding user response:

In int *first=0;, the 0 initializes first, not *first. That is, it sets the pointer to 0 (making a null pointer). A null pointer does not point to any object.

The C standard does not define the behavior of attempting to use a null pointer pointer to access an object, so the behavior of *first<second is not defined. A common result is the program attempts to access address zero, and the virtual memory of the process is configured so this causes a hardware exception to be generated, and the program is terminated.

(However, it is also possible that program optimization during compilation may transform an attempt to use a null pointer into other code, causing your program to behave in ways that are mysterious until you become familiar with the effects of optimization.)

To fix this, you need to set first to point to a proper object. You can do this by making it point to some other named object, such as:

int x;
int *first = &x;

or by allocating memory for it:

int *first = malloc(sizeof *first);
if (!first)
{
    fprintf(stderr, "Error, failed to allocate memory.\n");
    exit(EXIT_FAILURE);
}

If you allocate memory, then you usually want to release it later in the program, with free(first), after you are done using it.

CodePudding user response:

Have a look at your code

    int *first=0;
    int second=1;
    if(*first<second)
    {
        printf("Works\n");
    }

first is a pointer to int which points to address 0 (that is, a null pointer). So, reading memory at address 0 is not allowed as it does not point to any valid object, which produces a segfault. So, you need to give the pointer a valid address.

Instead you can try giving the pointer a valid address of a variable which contains the value 0

Like this ...

    int x = 0;
    int* first = &x;
    int second = 1;
    if(*first < second)
    {
        printf("Works\n");
    }

CodePudding user response:

In first, aren't I giving the value the pointer is pointing at?

No. You are giving the value of the pointer itself. The line

int *first=0;

is saying "first should be a pointer pointing to address 0.".

Then you try to access the value stored at that address which doesn't exist, causing undefined behavior.

CodePudding user response:

With int *first=0 you are declaring [first] as pointer and initializing the value of that pointer to 0. From that point, [first] is pointing to 0-th memory location, which is not at error. However, dereferencing pointer in this stage (reading from memory address = 0) is not allowed.

CodePudding user response:

try this

int *first=(int*)malloc(sizeof(int));
*first=0;
int second=1;
if(*first<second)
{
    printf("Works\n");
}
  • Related