Reputation: 21
Keep getting the error: [Warning] assignment makes pointer from integer without a cast for- l[i] = string[i]; and r[j] = string[j];
Could someone please explain why and suggest some changes that would make the code work without any errors? (I just started learning C lol I've been used to java so I'm kinda struggling using strings)
void split_string_at_index(char* string, int index, char* left, char* right) {
int size = sizeof(string);
char* r[MAX_SUBSTRING];
char* l[MAX_SUBSTRING];
int i,j;
for(i = 0; i<index; i++){
l[i] = string[i];
}
for(j = index; j<size;j++){
r[j] = string[j];
}
}
Upvotes: 0
Views: 491
Reputation: 2298
The primary issue here is a needed shift in mindset as C and Java are different languages and they are different enough here that it is tripping you up. There are additional errors beyond the warning message reported in the question.
In a sense there are no strings in C. C does not have a string type. Instead, it has a convention of how to handle buffers/arrays of char to act as strings. In particular, a null terminator is used to mark the end of the "string" or the currently used portion of the buffer.
So a C style string is an array/buffer that contains a sequence of characters terminated with a null character ('\0'). You are responsible for providing a big enough buffer to fit all the characters and the null terminator, not ever trying to stuff more characters into it than will fit, and keeping it null terminated.
Hopefully this makes clear that a char * variable is not a string or just like a string, though is used when dealing with C-style strings.
So in:
void split_string_at_index(char* string, int index, char* left, char* right) {
int size = sizeof(string);
your "string" is not a string, it is a pointer to char and its size has no relation whatsoever to the length of the string it gives you access to. You want:
int size = strlen(string);
Next:
char* r[MAX_SUBSTRING];
char* l[MAX_SUBSTRING];
you are trying to set up arrays of char to hold strings. Pointers don't come into it at this point. You want:
char r[MAX_SUBSTRING];
char l[MAX_SUBSTRING];
presuming that MAX_SUBSTRING is large enough to include all the chars you will ever try to stuff into them + 1 for the null terminator. You must include the + 1.
In the next part, the null terminator has been missed, so you want:
int i,j;
for(i = 0; i<index; i++){
l[i] = string[i];
}
l[index] = '\0';
In the next part for r, the null terminator is missing and the indexing is off.
for(j = index; j<size;j++){
r[j - index] = string[j];
}
r[size-index] = '\0';
}
Now that should take care of the string issue, but there are further issues. First, two of the parameters (left and right) are never used. Second, the r and l variables are local to the function and cease to exist when it ends. This means that your function is effectively a no-op. I presume that the intent was that l and r were to really be defined by callers of this function (not within it) and passed to the function as the left and right parameters. Further, that these parameters were to be used in the function where l and r currently are.
Upvotes: 1