1

I'm not familiar to pointers and I stumbled upon segmentation fault on my code, wheres if I don't use pointers this code runs perfectly.

#include <stdio.h>
#include <string.h>

int main()
{ 

    char *string[100],i,j;
    char *(*odd)[100];
    i = j = 0;

    fgets(*string, 100, stdin);

    while (*string[i] != '\0') {
        if (i % 2 == 0) {
                *odd[j++] = string[i];
        }
        i++;
    }

    *odd[j] = '\0';

    printf("Characters at odd position: %s\n",*odd[j]);

    return 0;
}

I'm guessing that I'm printing the odd array the wrong way, but I can't print it just using *odd as well.

5
  • 1
    What does *string[strlen(*string) - 1] = '\0'; do? Commented Aug 23, 2016 at 9:26
  • 1
    There are so many things wrong with that code that it's hard to know where to start. But one starting point might be this: Is string supposed to be a single string, or an array of strings? Right now it's the latter, an array of strings. Continue from there. Commented Aug 23, 2016 at 9:27
  • also *string[strlen(*string) - 1] = '\0'; this line is a little bit strange => you want to be sure to have null character at the end of string (ie you don't be sure that you have one) and for that you use strlen which look for a null character.... Commented Aug 23, 2016 at 9:32
  • @Garf365 I'm following this link but this is a non-pointer version. I wanted to convert it to full pointer version Commented Aug 23, 2016 at 9:43
  • the line *odd[j] = '\0'; is important! you have to put null character at the end of odd string Commented Aug 23, 2016 at 9:50

4 Answers 4

2

With pointer:

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

#define SIZEOF_BUFFER 100

int main(){ 
    char *string; // string is a pointer
    char i,j;

    char *odd; // odd is a pointer

    // allocate memory to be able to store data
    string = malloc(sizeof(char) * SIZEOF_BUFFER);

    if (string == NULL) {
        exit(-1);
    }

    odd = malloc(sizeof(char) * SIZEOF_BUFFER);

    if (od == NULL) {
        free(string);
        exit(-1);
    }

    i = j = 0;

    fgets(string, SIZEOF_BUFFER, stdin);

    while (string[i] != '\0') {
        if (i % 2 == 0) {
                odd[j++] = string[i];
        }
        i++;
    }

    odd[j] = '\0';

    printf("Characters at odd position: %s\n",odd);

    // Don't forget to free allocated memory
    free(odd);
    free(string);

    return 0;
}

Without pointer:

#include <stdio.h>
#include <string.h>

int main(){ 
    char string[100],i,j;

    char odd[100];

    i = j = 0;

    fgets(string, 100, stdin);

    while (string[i] != '\0') {
        if (i % 2 == 0) {
                odd[j++] = string[i];
        }
        i++;
    }

    odd[j] = '\0';

    printf("Characters at odd position: %s\n",odd);

    return 0;
}

also, no need of *string[strlen(*string) - 1] = '\0'; => fgets put always a null character at the end of string

Sign up to request clarification or add additional context in comments.

3 Comments

this, this is awesome! it solves what I'm looking for, and reminds me of freeing allocated memory as well !
can i ask you another question? the main purpose of odd[j] = '\0'; as you said fgets always put a null character, after i assign to the new arrays pointer without using fgets, so they are without '\0', are my understanding correct?
You're partially right: you stop iterating on string when you find \0 character, so you never put it in odd
2
char *string[100],i,j;
fgets(*string, 100, stdin);

You are trying to write to address at string[0] but it is not initialized. I guess you mean smth like

char string[100];
fgets(string, 100, stdin);

P.S. char *string[] is an array of pointers to char while char string[] is an array of chars, and for this case in expressions string decays to pointer to first element i.e. to pointer to char.

4 Comments

cant i fgets into array of *string[100]?
*string[100] is an array of pointer, not an array of string, nor a string
So transform char string[100] to char * string and allocate memory (see my answer)
@ChristopherTan You can not since such array contain pointers, not chars. But you can receive string into buffer that is pointed to one of array elements, e.g. into string[0]: fgets(string[0], ....). Note that this pointer must be properly initialized, i.e. it must point to valid buffer.
1

This is a problem with precedence of [] vs *. See http://en.cppreference.com/w/c/language/operator_precedence. To solve it do the dereferences like this:

(*odd)[j] = '\0';

This also applies to declarations; to declare string as a pointer to an array of 100 chars, change it to:

char (*string)[100];

You also must allocate memory. Here is a changed version of your code with all these changes:

#include <stdio.h>
#include <string.h>

int main(){
    char actualstring[100];
    char (*string)[100] = &actualstring, i = 0, j = 0;
    char actualodd[100];
    char (*odd)[100] = &actualodd;

    fgets(*string, 100, stdin);

    (*string)[strlen(*string) - 1] = '\0';

    while ((*string)[i] != '\0') {
        if (i % 2 == 0) {
                (*odd)[j++] = (*string)[i];
        }
        i++;
    }

    (*odd)[j] = '\0';

    printf("Characters at odd position: %s\n",*odd);

    return 0;
} 

You may find it useful to translate declarations to english and vice-versa at http://cdecl.org/

2 Comments

i changed to what u recommended two of the above but still segmentation fault (core dumped) occurs...
It's normal because you don't have allocated memory
0

string is an array of pointers. You did not initialize any of those pointers. But then you write through those pointers as if they pointed somewhere.

Your code is a more complicated version of:

char *ptr;
fgets(ptr, 100, stdin);

in which (hopefully) it should be obvious what the problem is. odd has a similar problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.