Ali
Ali

Reputation: 10463

segmentation fault for increment in the array index?

I'm trying to do my homework

Before I can use this line without any problem and no segmentation fault and I don't know why it doesn't work now after I compile my code.

This is how it calls from the main():

rb = ascii(back_data[i], phr);

This is how data define in the main():

char phr[41];

int ascii(const char back[ ], char data[ ]){
  int l = 0, n = 0, i = 0, co = 0;
  char binary_holder[8], char_set;

  binary_holder[0] = '\0';

  l = strlen(back);

  for (i = 0; i <= l; i++){

    if (back[i] == '0' || back[i] == '1' && co < 8){

        binary_holder[co] = back[i];
        co++;
    }
    if(co == 8){

      binary_holder[8] = '\0';
      co = 0;
    }

    char_set = strtol(binary_holder, NULL, 2);

    if (char_set > 31 && char_set != 127){
        data[n++] = char_set;   
    }


  }

I forgot to mention that the problem is here and I can't figure out still why

data[n++] = char_set;

Upvotes: 0

Views: 224

Answers (1)

Jens
Jens

Reputation: 72697

Looks like in ascii(back_data[i], phr); the first arg is a char instead of a pointer-to-char. This non-pointer is passed to strlen() — boom.

Upvotes: 3

Related Questions