Home > Blockchain >  A pointer returned by a method is not getting assigned properly to another pointer defined in C
A pointer returned by a method is not getting assigned properly to another pointer defined in C

Time:09-17

I am beginner to C programming. While working on a simple assignment, my code crashed up with a segmentation fault. When I debugged with gdb I founded that pointer returned from "manchester" method is different from assigned pointer "encoded" in main.

My code as below

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

char* manchester(char* bitstring);
char* differential_manchester(char* bitstring);

int main(){
    int method;
    scanf("%i\n", &method);   //scan the method to encode
    char* bitstring = (char *) malloc(100*sizeof(char)); // assumed max length = 100
    scanf("%s", bitstring);

    char* encoded;
    if(method == 0){
        char* encoded = manchester(bitstring); // where the confusion occur
    }
    else if (method == 1){
        char* encoded = differential_manchester(bitstring);
    }
    printf("%s", encoded);
    free(encoded);
    free(bitstring);

    return 0;
}

char* manchester(char* bitstring){
    char* encoded_st = (char*) malloc(200*sizeof(char));
    int i = 0, j = 0;
    while (bitstring[i] != '\0' ){
        if (bitstring[i  ] == '0'){
            encoded_st[j  ] = '1';
            encoded_st[j  ] = '0';
        }
        else{
            encoded_st[j  ] = '0';
            encoded_st[j  ] = '1';
        }
    }
    encoded_st[j  ] = 0; //append null character at end
    return encoded_st;
}

char* differential_manchester(char* bitstring){
    //TODO: yet to implement
    return NULL;
} 

gdb debugging

(gdb) b encode.c:14
Breakpoint 1 at 0x1222: file encode.c, line 14.
(gdb) run
Starting program: /home/ishad/Desktop/computer communication/a.out 
0
1010

Breakpoint 1, main () at encode.c:14
14      if(method == 0){
(gdb) n
15          char* encoded = manchester(bitstring);
(gdb) s
manchester (bitstring=0x7ffff7e5a2d4 <__GI___libc_malloc 116> "I\211\300H\205\300\017\204\300") at encode.c:27
27  char* manchester(char* bitstring){
(gdb) n
28      char* encoded_st = (char*) malloc(200*sizeof(char));
(gdb) n
29      int i = 0, j = 0;
(gdb) n
30      while (bitstring[i] != '\0' ){
(gdb) n
31          if (bitstring[i  ] == '0'){
(gdb) n
36              encoded_st[j  ] = '0';

...

(gdb) n
30      while (bitstring[i] != '\0' ){
(gdb) n
40      encoded_st[j  ] = 0; //append null character at end
(gdb) n
41      return encoded_st;
(gdb) p encoded_st
$1 = 0x555555559720 "01100110"
(gdb) n
42  }
(gdb) n
main () at encode.c:20
20      printf("%s", encoded);
(gdb) p encoded
$2 = 0x7fffffffdec0 "\001"

my question is why encoded_st pointer is different from encoded pointer. I tried to find reason using several key words. But I didn't find a similar question. :(

CodePudding user response:

You redeclared the variable with the same name encoded in the scope of the if statement

char* encoded;
if(method == 0){
    char* encoded = manchester(bitstring); // where the confusion occur
}
else if (method == 1){
    char* encoded = differential_manchester(bitstring);
}

So the variable encoded declared in the outer scope relative to the if statement stays uninitialized.

Remove the declarations within the if statement

char* encoded;
if(method == 0){
    encoded = manchester(bitstring); // where the confusion occur
}
else if (method == 1){
    encoded = differential_manchester(bitstring);
}
  • Related