Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting warning: (near initialization for ‘ptr’) and segmentation fault at runtime when accsessing value at pointer?

This is the following code: Why it is giving segmentation fault when I try to access first value of array? What are all this warnings?

#include<stdio.h>
int main(void)
{
    int *ptr = {1,2,3,4,5};//Is it not similar to char *ptr="Stackoverflow"?
    printf("%d\n",*ptr);// why Segmentation fault(core dumped) instead of 1
    return 0;
}

...
output:

warning: initialization makes pointer from integer without a cast [enabled by default] 
int *ptr = {1,2,3,4,5};
^

warning: (near initialization for ‘ptr’) [enabled by default]
warning: excess elements in scalar initializer [enabled by default]
warning: (near initialization for ‘ptr’) [enabled by default]
warning: excess elements in scalar initializer [enabled by default]
warning: (near initialization for ‘ptr’) [enabled by default]
warning: excess elements in scalar initializer [enabled by default]
warning: (near initialization for ‘ptr’) [enabled by default]
warning: excess elements in scalar initializer [enabled by default]
warning: (near initialization for ‘ptr’) [enabled by default]
like image 989
geetesh singh Avatar asked Oct 12 '25 19:10

geetesh singh


1 Answers

//Is it not similar to char *ptr="Stackoverflow"?

TL;DR No, it is not.


The used initializer, {1,2,3,4,5} is called a brace-enclosed initalizer which is supposed to initialize the values of the type of the elements. This is used for aggregate or union type type, like mentioned as in C11, chapter §6.7.9, Initialization

the initializer for an object that has aggregate or union type shall be a brace enclosed list of initializers for the elements or named members.

Here, the initializer list contains all int values, and you're trying to initialize a pointer thought it. This is wrong.

Also, regarding the scalar type, quoting C11, chapter §6.2.5

Arithmetic types and pointer types are collectively called scalar types.[...]

and the aggregate types

[...]Array and structure types are collectively called aggregate types.

There are many issues here, like

  1. You're using int value to initialize an int *.
  2. You're ending up supplying a brace enclosed list containing more than one initializer element for a scalar object.

So, later in your code,

 printf("%d\n",*ptr);

is essentially an invalid memory access, which invokes undefined behavior. The segmentation fault is one of the many side-effects.

Coming to the point of the comment,

char *ptr="Stackoverflow"?

In case of char *ptr="Stackoverflow";, here, "Stackoverflow" is called a string literal and ptr is initalized with the base address of the string literal.


Solution:

You need to have an array of ints which you can initialize using the brace-enclosed initializer. Something along the line of

 int ptr[] = {1,2,3,4,5};

will be valid. Then you can use it like

 for(int i = 0; i < 5; i++)
    printf("%d\t", *(ptr+i));
like image 135
Sourav Ghosh Avatar answered Oct 14 '25 10:10

Sourav Ghosh