user2923786
user2923786

Reputation: 11

addition of numbers using pointers

When I want to run the below code, I am getting the following error.

Enter number1
4
Segmentation fault (core dumped)


#include<stdio.h>
#include<stdlib.h>

int main()
{
  int *p1;int *p2;
  int n;
  printf("Enter number1\n");  
  scanf("%d",p1);
  printf("Enter number2\n");
  scanf("%d",p2);
  printf("sum:%d\n", (*p1 + *p2));
  return 0;
}

Upvotes: 1

Views: 113

Answers (7)

P.P
P.P

Reputation: 121387

For whatever you want to use pointers (and hence use dynamically allocated memory). Allocate memory and initialize the pointers:

#include<stdio.h>
#include<stdlib.h>

int main()
{
int *p1;int *p2;
int n;

p1=malloc(sizeof(int)); 
p2=malloc(sizeof(int));

printf("Enter number1\n");  
scanf("%d",p1);
printf("Enter number2\n");
scanf("%d",p2);
printf("sum:%d\n", (*p1 + *p2));
return 0;
}

Upvotes: 1

atk
atk

Reputation: 9314

Int *p1 is a pointer to an integer. You never initialize it, so itoints to a 'random' location in memory. When you try to scanf() into p2, you are writing to wherever p1 happens to point. Since p1 points to an invalid location, you get undefined behavior. In this case, you get a seg fault because the location where p1 points is outside of allocated space.

You can fix this by allocating memory for p1. Somethin glike...

int *p1 = malloc(sizeof(int));

Or you can make p1 just an int instead of a pointer to the int, and pass the pointer to p1 when you czll scanf...

int p1;
...
scanf(...,&p1);

Upvotes: 0

ryyker
ryyker

Reputation: 23218

Changes in your code include changing declarations to int, instead of *int, and passing the pointer to these variables using the &:

#include<stdio.h>
#include<stdlib.h>

int main()
{
int p1;int p2;
int n;
printf("Enter number1\n");  
scanf("%d",&p1);
printf("Enter number2\n");
scanf("%d",&p2);
printf("sum:%d\n", (p1 + p2));
return 0;
} 

Upvotes: 2

jan
jan

Reputation: 221

Just use regular int, not a pointer (not needed here).

#include<stdio.h>

int main()
{
    int p1;int p2;
    int n;
    printf("Enter number1\n");  
    scanf("%d",&p1);
    printf("Enter number2\n");
    scanf("%d",&p2);
    printf("sum:%d\n", (p1 + p2));
    return 0;
}

Upvotes: 0

Brian Cain
Brian Cain

Reputation: 14619

These pointers were not initialized:

int *p1;int *p2;

...therefore, they don't have any memory allocated to them and there's no way of knowing where they do point.

scanf expects to be able to write to these locations. You should instead use int p1; int p2; and pass the address of these to scanf:

scanf("%d", &p1);

Upvotes: 1

digital_revenant
digital_revenant

Reputation: 3324

Your pointers are uninitialized. Allocate memory for them before using otherwise it is undefined behavior.

p1 = malloc(sizeof(int));
p2 = malloc(sizeof(int));

Better still, just use int instead of an int *

int p1, p2;
int n;
printf("Enter number1\n");  
scanf("%d",&p1);
printf("Enter number2\n");
scanf("%d",&p2);

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 224944

Your scanf calls cause undefined behaviour - you're passing uninitialized pointers. What you meant is probably something like:

int p1, p2;

...

scanf("%d", &p1);

...

scanf("%d", &p2);

printf("sum:%d\n", p1 + p2);

Upvotes: 0

Related Questions