-3
#include <stdio.h>

#define null1 ((void*)0)
#define val ((int)2)

int main() {
    int *p;
    p = null1;
    printf("%p", &p);
    //p = (int *)val;
    *p = val;
    //printf("\n%p", (int*)p);
    return 0;
}

Output:

Segmentation fault(core dumped)

I want to assign value of macro to the Pointer.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
eddyctid
  • 11
  • 1
  • Possible duplicate of [What is a segmentation fault?](https://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault) – Prostagma Jul 27 '19 at 07:57
  • 1
    `p` is initialised to a null pointer (disguised a little by your usage of macros). Evaluating `*p` has undefined behaviour. The statement `*p = val` both evaluates `*p` to obtain an address, and assigns that address to the value of `2`, which overwrites memory that your program should not access. The host system detects that, and sends a signal to your program, which causes the program to terminates with a message about a segmentation fault. One possible fix - create an `int` and assign `p` the address of that e.g. `int i; p = &i;` before the first `printf()` – Peter Jul 27 '19 at 08:18
  • It is unclear what you want't to do. Do you want `p` to point to somewhere that holds the value `2`, or do you want to force `p` to hold an integer, rather than being a pointer? – HAL9000 Jul 27 '19 at 09:45
  • @eddyctid: you can accept one of the answers by clicking on the grey checkmark below its score. – chqrlie Aug 11 '19 at 15:52

5 Answers5

2

null1 is a null pointer, its definition #define null1 ((void*)0) is one of the accepted definitions for a null pointer.

val is a macro expanding to the plain integer constant 2. Casting 2 as (int) has no effect, 2 is already an int constant value.

Storing a value to a null pointer has undefined behavior. A segmentation fault is one the possible effect of undefined behavior. A rather useful one since it allows the debugger to point to the problem immediately.

Casting any other integer value as (int *) has undefined behavior too: p = (int *)val; *p = val; is very likely to cause the same segmentation fault, but may have some other unexpected side effects, depending on the target platform specifics: if 2 happens to be the address of the self-destruct trigger port, all bets are off.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
1

Null pointer is a special reserved value of a pointer. A pointer of any type has such a reserved value. Formally, each specific pointer type (int *, char * etc.) has its own dedicated null-pointer value. Conceptually, when a pointer has that null value it is not pointing anywhere.

Void pointer is a specific pointer type - void * - a pointer that points to some data location in storage, which doesn't have any specific type.

Here null1 refers to a void pointer whereas p is a pointer of the type integer.

Hope I was right and this is helpful to you.

0

Pointers directly point to memory addresses. While each program that we run is allocated a fixed memory area only (called segments). All memory address in this area is referred to using relative memory address.

But pointers have absolute memory addresses in them. Generally, the starting memory addresses are for Operating System's use. When you assign a pointer to some address outside of its segment area then it gives SIGSEGV ( Segmentation Error ).

In your case also, you are trying to assign p an address outside its segment area thus the error.

0

You just need to assign your pointer to your macro as code below:

#include <stdio.h>

#define val 2

int main()
{
    int *p;

    p = (int*)val;
    printf("%p\n", &p);
    printf("%d\n", p);

    return 0;
}
Mojtaba Ahmadi
  • 1,044
  • 19
  • 38
0

NULL pointer is an invalid pointer because it points to memory that is not valid to access so that the statment *p = val is not valid, so the Segmentation fault error appears.

To overcome this error you need to assign a valid address of a memory location to the pointer(p), such as defining a variable (var) and assign the addres to the pointer as p = &var;.

Ahmed Yasen
  • 145
  • 2
  • 13