Segmentation fault in C. Trying to swap two values using pointer

Gaurav

I am getting a segmentation fault while trying to attempt swapping values in two variables. My code is :

void swap(int *a,int *b){
    int *temp;
    *temp=*a;
    *a=*b;
    *b=*temp;
}
int main(){
    int i=1,j=0;
    printf("Before %d,%d\n",i,j);
    swap(&i,&j);
    printf("After %d,%d\n",i,j);
    return 0;
}

I am getting the following error:

Before 1,0
After 0,1
Segmentation fault (core dumped)

What looks mysterious to me is the error is being produced after the values have been swapped successfully. What is the bug? Do I need to typecast the pointers anywhere?

Ilya

Your pointer int *temp; points on nothing.

So, when your program does *temp=*a;, it puts value of a into random memory block.

Try this fix:

void swap(int *a,int *b){
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}

Updated:

Additional question:

Suppose I want to use temp as a pointer variable and not as a regular variable, is there any way to get my program executed or I need to leave my stubbornness?

Answer: you can try this:

void swap(int *a,int *b){
    int *temp;
    temp = malloc(sizeof(int));
    if (temp == NULL)
      return;
    *temp=*a;
    *a=*b;
    *b=*temp;
    free(temp);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Segmentation fault in C. Trying to swap two values using pointer

From Dev

segmentation fault when trying to deference pointer : C

From Dev

segmentation fault when trying to deference pointer : C

From Dev

segmentation fault using char pointer (C)

From Dev

C array pointer segmentation fault

From Java

Segmentation Fault when trying to access pointer to Node

From Dev

Segmentation fault when trying to access pointer in struct

From Dev

Segmentation fault when trying to access pointer in struct

From Dev

Segmentation fault in C while using int pointer argument

From Dev

C segmentation fault when using pointer to structure in a linked list

From Dev

C segmentation fault when using pointer to structure in a linked list

From Dev

segmentation fault in c about using malloc, and char array pointer

From Dev

Segmentation fault while using pointer to pointer to a char

From Dev

Pointer-to-a-pointer in C throws segmentation fault

From Dev

Pointer Pointer leads to Segmentation Fault in C

From Dev

Segmentation Fault using stack and frame pointer

From Dev

Odd C pointer segmentation fault errors

From Dev

Why this pointer got a Segmentation fault C?

From Dev

Segmentation fault error with pointer array in C

From Dev

Segmentation Fault using strtok() in C

From Dev

C Segmentation fault using strtok

From Dev

Segmentation fault in C using MinGW

From Dev

Segmentation Fault using free() in c

From Dev

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

From Dev

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

From Dev

Program gives segmentation fault while trying to malloc for a pointer member of structure

From Dev

Passing pointer to function causes segmentation fault when trying to retrieve value

From Dev

Segmentation fault while trying to print parts of a pointer struct

From Dev

segmentation fault when trying to dereference a dynamically allocated pointer

Related Related

  1. 1

    Segmentation fault in C. Trying to swap two values using pointer

  2. 2

    segmentation fault when trying to deference pointer : C

  3. 3

    segmentation fault when trying to deference pointer : C

  4. 4

    segmentation fault using char pointer (C)

  5. 5

    C array pointer segmentation fault

  6. 6

    Segmentation Fault when trying to access pointer to Node

  7. 7

    Segmentation fault when trying to access pointer in struct

  8. 8

    Segmentation fault when trying to access pointer in struct

  9. 9

    Segmentation fault in C while using int pointer argument

  10. 10

    C segmentation fault when using pointer to structure in a linked list

  11. 11

    C segmentation fault when using pointer to structure in a linked list

  12. 12

    segmentation fault in c about using malloc, and char array pointer

  13. 13

    Segmentation fault while using pointer to pointer to a char

  14. 14

    Pointer-to-a-pointer in C throws segmentation fault

  15. 15

    Pointer Pointer leads to Segmentation Fault in C

  16. 16

    Segmentation Fault using stack and frame pointer

  17. 17

    Odd C pointer segmentation fault errors

  18. 18

    Why this pointer got a Segmentation fault C?

  19. 19

    Segmentation fault error with pointer array in C

  20. 20

    Segmentation Fault using strtok() in C

  21. 21

    C Segmentation fault using strtok

  22. 22

    Segmentation fault in C using MinGW

  23. 23

    Segmentation Fault using free() in c

  24. 24

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

  25. 25

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

  26. 26

    Program gives segmentation fault while trying to malloc for a pointer member of structure

  27. 27

    Passing pointer to function causes segmentation fault when trying to retrieve value

  28. 28

    Segmentation fault while trying to print parts of a pointer struct

  29. 29

    segmentation fault when trying to dereference a dynamically allocated pointer

HotTag

Archive