0

I am getting a problem with pointers in 32-bit architecture.I want to add two numbers using only pointer variables for ex: int *a,*b,*add; but in 32-bit based compiler such as codeblocks doesn't giving the ouptut.

But in case of 16-bit based compiler such as turbo c++ it is working properly.

This is the code that I've tried

int *a,*b,*add;
printf("Enter two no.");
scanf("%d%d",a,b);
*add=*a+*b;

in code block it is returning nothing . but in turbo c ++ it is working properly.

2
  • 2
    codeblocks is not a compiler, and scanf wants pointers to an allocated space: use int a = malloc(sizeof *a); before scanf Commented May 26, 2019 at 10:00
  • you can do it like this: *a+=*b; Commented May 26, 2019 at 11:57

2 Answers 2

2

scanf("%d%d",a,b); tells scanf to read numerals from input and write their values to the places in memory where a and b point. But a and b do not properly point to anywhere because you did not initialize them. Neither their definition in int *a,*b,*add; nor their use in scanf sets a value for where they point to.

To make a, b, or add point to an actual int object, you must define or allocate an int object and then assign its address to a, b, or add.

For example, you can use:

int x, y, z;
int *a = x, *b = y, *add = z;

Or:

int *a, *b, *add;
a = malloc(sizeof *a);
b = malloc(sizeof *b);
add = malloc(sizeof *add);
if (!a || !b || !c)
{
    fprintf(stderr, "Error, memory allocation failed.\n");
    exit(EXIT_FAILURE); // Include <stdlib.h> for this.
}

If your code “works” in Turbo C++, it may be because the uninitialized a, b, and add take on values that happen to be in memory that happened to point to usable memory. This is not reliable behavior, and it could easily break if any changes are made to the program. Always initialize pointers and other objects before you use them.

When you are done using memory allocated with malloc, you should generally release it, which you can do with free:

free(add);
free(b);
free(a);
Sign up to request clarification or add additional context in comments.

3 Comments

Or better int * temp;temp = malloc(sizeof *a);/*Error checking*/a=temp;
@sjsam: Introducing an unnecessary additional object is not better. “Temporary” objects may be needed for realloc, where you want to keep the original pointer in case the allocation fails, but they are unnecessary for malloc.
Hmm... Since the has to do multiple allocations, they can, perhaps, think of a function to the allocation and an assert. In any case, error checking is inevitable.
2

You need to allocate the memory for variables at first. When you only call "int *a" you don't have the memory reserved. So you need to call malloc or calloc.

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

int main(void) {
  int *a, *b, *add;

  // allocate memory
  a = malloc(sizeof(int));
  b = malloc(sizeof(int));
  add = malloc(sizeof(int));

  // check if there was an error allocating the memory
  if (a == NULL) {perror("allocate pointer a");};
  if (b == NULL) {perror("allocate pointer b");};
  if (add == NULL) {perror("allocate pointer add");};

  // read numbers
  printf("Enter two numbers ");
  scanf("%d%d", a, b);

  printf("a: %d; b:  %d\n", *a, *b);

  // calculate the result and print it
  *add = *a + *b;
  printf("sum: %d\n", *add);

  // free the pointer
  free(a);
  free(b);
  free(add);

  return 0;
}

You should also check if your pointer is null after allocating because then you have not enough space for the variable and you should exit the program. You could also malloc and check for error in one line this would look for example like: if ((a = malloc(sizeof(int))) == NULL) {perror("allocate pointer a");};

When you don't need you pointer you should free them or you will get memory leaks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.