I've been asked to do this:
Make a
skip_spaces()function accepting a strings, that returns a reference to the first element in the array that is not a space character (if the string is only composed of spaces, the pointer will address the null terminator\0). Then make a main program principal calling this function with a string read onstdin. From the given result, the program will then display the string from the first non-spacechar."
I've only started using pointers, and I'm clearly not a C expert, so I'm pretty lost here. Here is what I got so far:
In skip_spaces.c I have:
char *skip_spaces(char *s[]) {
char *ref = '\0';
int i = 0;
while (*s[i] != '\0') {
if (*s[i] == ' '):
i++;
else {
*ref = *s[i];
}
}
}
In skip_spaces.h I have:
char *skip_spaces(char *);
And my main program:
#include "skip_spaces.h"
#include <stdio.h>
int main(void) {
int input;
char *str[30];
char *spaceless;
printf("input string : ");
input = scanf("%s", str);
if (input == 1) {
int i = 0;
spaceless = skip_spaces(str);
printf("modified string : %s.", spaceless);
return -1;
}
Now, I'm not sure yet if the program even does what I want it to do.
My issue here is that I can't even test it out at this point: I've tried a loooot of stuff, I can never compile properly, whenever I fix an issue somewhere, I get another issue else where. Pretty much all errors come from my main program.
I have two very persistent errors:
error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’
This error points at my input = scanf line, more precisely to my str var
skip_spaces.h:1:8: note: expected ‘char *’ but argument is of type ‘char **’
I've tried fidgetting, placing * here and & there, but either I have these 2 errors, either I have a lot more others...
I've even found several working codes for this exact function on the Internet (most were from SO actually), but whenever I try to implement their working solution in my code, I get these errors again. 100% certain my problem comes from my comprehension of pointers. I hope someone can shed some light.