Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Invalid memory access when trying to dereference a pointer obtained through a function call
Post
Invalid memory access when trying to dereference a pointer obtained through a function call
+4
−1
Hey, regarding a question on returning pointers as functions, why does my code produce this error & won't print out the sum = 6?
#include<stdio.h>
#include<stdlib.h>
int *Add(int *a, int *b){
int c = (*a) + (*b);
return &c;
}
int main (){
int x=2, y=4;
int *Ptr = Add(&x,&y);
printf("Sum = %d\n",*Ptr);
}
1 comment thread