Pointing to a variable using pointers in c-segmentation fault

User 1426833

I wanted the pointer p to point to the value of (var-1).Please suggest a way to solve this error.

  int var = 10;
  int *p;
  *p =var-1;
  printf("%d",*p);
Vlad from Moscow

To point to the value of the expression (var-1) you need an object that will store the value.

So the only approach provided that the variable var itself shall not be changed is the following

  int var = 10;
  int var2 = var-1;
  int *p = &var2;
  printf("%d",*p);

Or you could allocate an object of the type int dynamically where the result of the expression will be stored like

  int var = 10;
  int *p = malloc( sizeof( int ) );
  *p = var-1;
  printf("%d",*p);
  //...
  free( p );

According to the C Standard (6.5.3.2 Address and indirection operators)

1 The operand of the unary & operator shall be either a function designator, the result of a [] or unary * operator, or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier.

And

4 The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object. If the operand has type ‘‘pointer to type’’, the result has type ‘‘type’’. If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined.

In the original code snippet you got a segmentation fault because the pointer p does not point to a valid object and has indeterminate value because it was not initialized.

int *p;
*p =var-1;

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

C Segmentation fault using strtok

분류에서Dev

Segmentation fault using realloc on large arrays in C

분류에서Dev

Resetting Variable : Segmentation fault

분류에서Dev

A* Implementation in C, Segmentation fault

분류에서Dev

Segmentation Fault - GNU C

분류에서Dev

Strcpy Segmentation Fault C

분류에서Dev

C - trying to assign return value from a function to a variable causes segmentation fault

분류에서Dev

Segmentation fault in recursive Binary Search Algorithm in C

분류에서Dev

merge sort segmentation fault c++

분류에서Dev

C segmentation fault 11 on recv() function

분류에서Dev

Segmentation fault when trying to clone a function (in C)

분류에서Dev

Segmentation fault when signing a message using OpenSSL, SWIG, and Perl

분류에서Dev

Can't find where is the segmentation fault in C program

분류에서Dev

Segmentation Fault in C causing code to run sometimes but not other times

분류에서Dev

Pthread_create causing segmentation fault (C++, Kubutnu 15)

분류에서Dev

c++ unsigned char array allocation - segmentation fault

분류에서Dev

Segmentation fault in Assembly and string

분류에서Dev

Access Violation (Segmentation Fault)

분류에서Dev

Malloc to struct; segmentation fault

분류에서Dev

Segmentation Fault in hsearch

분류에서Dev

Segmentation Fault? Why?

분류에서Dev

Strange segmentation fault in code

분류에서Dev

Segmentation Fault on return statement

분류에서Dev

glGenBuffers crashing with Segmentation fault

분류에서Dev

Struct causing segmentation fault

분류에서Dev

Segmentation fault in sorting algorithm

분류에서Dev

Segmentation Fault While Sorting - Malloc

분류에서Dev

Fractional Knapsack Algorithm segmentation fault

분류에서Dev

Segmentation fault on reboot Ubuntu 12.04

Related 관련 기사

뜨겁다태그

보관