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.
Post History
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 *A...
#5: Post edited
Question on pointers as return functions.
- Invalid memory access when trying to dereference a pointer obtained through a function call
#4: Post edited
Question on pointers as return functions.
- Hey, regarding a question on returning pointers as functions, why does my code produce this error & won't print out the sum = 6?
- ![Code error](https://software.codidact.com/uploads/U8qbUvf8H6Rb5XyCNoK9m8yj)
- ![Pop-up error message](https://software.codidact.com/uploads/FfBWyaPMMk331GGGS4qjeyd1)
`#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",*Ptr);}`Also, how do you even format the above to become a code in Codidact? X'D
- Hey, regarding a question on returning pointers as functions, why does my code produce this error & won't print out the sum = 6?
- ![Code error](https://software.codidact.com/uploads/U8qbUvf8H6Rb5XyCNoK9m8yj)
- ![Pop-up error message](https://software.codidact.com/uploads/FfBWyaPMMk331GGGS4qjeyd1)
- ```c
- #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
- ",*Ptr);
- }
- ```
#3: Post edited
- Hey, regarding a question on returning pointers as functions, why does my code produce this error & won't print out the sum = 6?
- ![Code error](https://software.codidact.com/uploads/U8qbUvf8H6Rb5XyCNoK9m8yj)
- `#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);
- }`
- Also, how do you even format the above to become a code in Codidact? X'D
- Hey, regarding a question on returning pointers as functions, why does my code produce this error & won't print out the sum = 6?
- ![Code error](https://software.codidact.com/uploads/U8qbUvf8H6Rb5XyCNoK9m8yj)
- ![Pop-up error message](https://software.codidact.com/uploads/FfBWyaPMMk331GGGS4qjeyd1)
- `#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);
- }`
- Also, how do you even format the above to become a code in Codidact? X'D
#2: Post edited
- 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);
- }`
- Also, how do you even format the above to become a code in Codidact? X'D
- Hey, regarding a question on returning pointers as functions, why does my code produce this error & won't print out the sum = 6?
- ![Code error](https://software.codidact.com/uploads/U8qbUvf8H6Rb5XyCNoK9m8yj)
- `#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);
- }`
- Also, how do you even format the above to become a code in Codidact? X'D
#1: Initial revision
Question on pointers as return functions.
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); }` Also, how do you even format the above to become a code in Codidact? X'D