2

Please look at the following code snippet -

int main(){

   int  *i_ptr = 5;
   printf("%d", *i_ptr); 

   return 0;
}

Here I am trying to declare and initialize a pointer variable i_ptr. It gives me the following warning while it compiles fine -

warning: initialization makes pointer from integer without a cast [enabled by default]

But when I am going to execute the code it gives me the following error -

Segmentation fault (core dumped)

I know a correct way to do this -

int n = 5;
int *ptr = &n;  

Now I have some questions -

1. While the first code fails at execution time why it doesn't give compilation error, instead of warning?

2. Can we initialize and declare pointer variable like this -

int n = 5 // both declaration and initialization of int type variable n  

Thanks in advance.

Razib
  • 10,965
  • 11
  • 53
  • 80

6 Answers6

3
  1. While the first code fails at execution time why it doesn't give compilation error, instead of warning?

The difference between errors and warnings depends on the flags you pass to your compiler.

It's a good idea to make the setting more paranoid during development (-Wall, -Wextra, possibly even -Weverything with Clang), in addition to making warnings into errors via -Werror (which you probably shouldn't set by default when shipping code).

  1. Can we initialize and declare pointer variable like this

Pointer variables store addresses, and you need to declare an addressable something where you can store the value 5.

Since C99, it's possible to use a compound literal instead of a named variable:

int *p = &(int){ 5 };
Christoph
  • 164,997
  • 36
  • 182
  • 240
2

The * has a different meaning in your code.

It's not for pointer dereference but for pointer declaration, so you are declaring a pointer and assigning an integer to it, the pointer will store the integer as the address it points to.

That would presumably be Undefined Behavior because you don't know whether 5 a valid address or not, and it's very very likely that it's not.

If you split the declaration from the initialization it will become obvious that the code has a different meaning, example

int *pointer; /* here the `*' is used for pointer declaration */
int  value;

pointer  = &vaule; /* we store the address of value */
*pointer = 5; /* we dereference the pointer and write to that location */
/* ^ here the `*' is used for pointer dereference */

printf("%d\n", value * 5); /* this will print 25 */
/*                   ^ here the `*' is used for multiplication */

Now you see that I said that your code is confusing because

int *pointer = 5;

seems equivalent to

*pointer = 5;

but it's not.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1
  1. There is technically nothing wrong with int *n = 5. It's a valid pointer that points at memory with address 5 and that's why it lets you compile it. The reason it segfaults is that your program don't have access to this memory.
Razib
  • 10,965
  • 11
  • 53
  • 80
  • 1
    "Nothing wrong"? It is flat out *illegal* in both C and C++ language to initialize a pointer value with non-zero integer. The code is ill-formed. Such initialization (if you need it for some reason) requires an explicit cast. – AnT stands with Russia Apr 01 '15 at 18:52
  • In no way am I going to defend initializing pointer to static integer values. I merely stated that there's "Technically" nothing wrong with it. That's why it gets compiled, and why compiler prints Warning about it. – Martin Kalcok Apr 01 '15 at 19:04
  • @MartinKalcok: it is technically wrong because it does not follow the rules of the C language as specified by the ISO standard (cf C11 6.5.16.1, which also holds for initialization according to 6.7.9 §11) – Christoph Apr 01 '15 at 19:11
1

When you are doing

int  *i_ptr = 5;

you are assigning address 5 to i_ptr.But since this address doesn't belongs to your program,hence when you De-reference i_ptr it is giving you segmentation fault.Only try to access those memory location which belongs to your program.

avinash pandey
  • 1,321
  • 2
  • 11
  • 15
1

The compiler is not required to make it an error, so it does not.

The syntax you are looking for is: int *i_ptr = new int(5);

This allocates new memory large enough for an integer, sets the value to 5, and sets i_ptr to point at that memory. Remember to do delete i_ptr; whene you are done with the memory.

Bill
  • 14,257
  • 4
  • 43
  • 55
1

You code is illegal in both C and C++ languages. You are not allowed to initialize pointers with integer values. This

int *i_ptr = 5;

is illegal. It is not "assigning address 5 to a pointer" as some other answers seem to suggest. The above initialization is plainly non-compilable within the realm of standard C and C++ languages.

The diagnostic message that you received indicates an error in your code. After that, neither C nor C++ makes any guarantees about the meaning of your code, i.e. your code is neither C nor C++.

Don't ignore diagnostic messages issued by your compiler. C and C++ does no differentiate between "warnings" or "errors". Any diagnostic message should be treated as a potential error. It is up to you to recognize errors among what looks like a "mere warning". With GCC you can also try using -pedantic-errors option to make it report errors as errors.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765