0

I've hit a roadblock, when trying to manipulate strings when "storing" them as pointer variables.

// Sample code to input a string till line break is encountered, later re-allocating to save space
char *s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);

// Code to replace whitespaces with newlines
for (char *c = s; *c != '\0'; c++) {
    if (*c == ' ') {
        *c = '\n';
    }
}

printf("%s", s);

I want an explanation of char *c = s in the above code snippet, because when I tried to use the same concept to iterate over the string characters in another example (below) I was greeted with Segmentation fault runtime error.

I want to find the frequency of digits (0 to 9) from a string entered by the user:

int count[10];
char *s = malloc(1024 * sizeof(char));
scanf("%[^\n]", &s);
s = realloc(s, strlen(s) + 1);

for(char *c = s; *c != '\0'; c++) {  // Shows Segementation fault here
    if(*c >= '0' && *c <= '9') {
        count[*c - '0'] += 1;  // I am still trying to work this
    }
}
9
  • 1
    Please, don't show us working code, show the one which is not working. Commented Nov 14, 2019 at 21:06
  • Edited as requested. Commented Nov 14, 2019 at 21:22
  • Well, your count is uninitialized. This is one problem. The other is already answered down there... Commented Nov 14, 2019 at 21:27
  • Why malloc followed by realloc ? And by the way s = realloc(s, ...) is an anti-pattern. If realloc fails you lose your original pointer and leak memory. Commented Nov 14, 2019 at 21:30
  • @John3136 I would say it's an attempt to save memory by shrinking it to only what is needed. Commented Nov 14, 2019 at 21:31

2 Answers 2

1

In scanf you are using &s instead of s.

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

Comments

0

explanation of char *c = s:

in this statement, the pointer c is set to the same address as the pointer s for the first iteration of the for-loop. That means, c and s point to the same memory location. Then, for each further iteration of the for-loop, c is incremented (c++) and the for loop is executed as long as the dereferenced memory location (the content at the memory location) is not the null character.

Now for the segmentation fault, as you didn't post the non-working code, I can only guess. One guess could be that there is no null character inside the allocated memory, so the for loop is not stopping soon enough and increments the c pointer to an address higher than allocated with malloc. By dereferencing it you access a memory location outside the allocated memory. This can produce a segmentation fault.

Edit: ah, now I see you added the non-working code. Yes, Akshay's answer is right and the segmentation fault could already happen there, as you don't scanf to s but to the memory location where the pointer variable is saved. The array pointed by s is still uninitialized (unknown values) but you search for the first occurence of the null character with strlen. Now, realloc may fail and thus return NULL and then you would set your c pointer to NULL. That would also produce a segmentation fault.

1 Comment

Great explanation, precisely what I wanted to know and understand.

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.