Ok, I know this will probably be flagged as a duplicate, but I've searched everywhere and not been able to figure this out, so please bear with me.
Let me start off by saying that this is for a homework assignment, so I cannot change the function parameter types.
Also, I am not allowed to use any libraries (well, inside the function that does the changing).
Part 1
First, how would I accomplish this:
void changeMe(char* s, int size) {
//Change s to be "Hello, World!"
}
int main(void) {
char* c;
changeMe(c, 13);
printf("%s", c); //Print out "Hello, World!"
}
(The homework doesn't call for the "Hello, World!" bit, but I don't need help with the logic, just changing the passed variable.)
I have looked all over, and most answers end up like this one here, which says that I need the parameter to be char** s
, or char*& s
in order to change it.
Is my only choice to assume that enough memory is already allocated to s before it is passed? For example:
void changeMe(char* s, int size) {
int i;
for (i = 0; i < size - 1; i++) s[i] = 'a';
s[size - 1] = '\0';
}
int main(void) {
char* c = (char*) malloc(10 * sizeof(char)) ;
changeMe(c, 10);
printf("%s", c); //Prints out "aaaaaaaaa"
}
This works fine, but removing the malloc(...)
from the declaration of c
doesn't.
Putting malloc(...)
inside of the changeMe(...)
function works locally, but only lasts as long as changeMe
is in scope.
Is there a way to change my char* c
without assuming it has enough memory allocated to it before it is passed to the function?
Part 2
This pretty much falls right into the same exact category as what is above, so I will do a bit less explaining here.
The function:
void split(char* s, char** sub, int max, char sep) {
//Split string s by sep char, store all substrings found into sub
//sub will have at most max substrings in it.
}
I am not asking for help writing the logic of finding and splitting up char* s
by char c
. I can do that. My issue is primarily the same as above. How do I allocate memory to char** sub
so that it can fit all of the substrings in it (and, of course, change the variable passed).
For example:
void split(char* s, char** sub, int max, char sep) {
//-->(Needed) Make sub able to hold max strings.
//(Not Needed) Logic for splitting s up by sep, creating a substring.
//-->(Needed) Making sub[i] have memory to hold current substring.
}
int main(void) {
char* s = "Some String To Use";
char** subs;
split(s, subs, 10, ' ');
//subs should look like this:
//{"Some", "String", "To", "Use"}
}
I don't need help doing the actual work, I am just confused as to how to pass subs
into split(...)
and have it allocate the appropriate memory and change subs
.
Fin
If you have made it this far through my question, thank you for reading.
And, before all of the "did you try google" comments come in, yes. Here some of what I have looked at so far:
(Edit: Thanks, haccks, for adding hyperlinks for me.)
- netcom
- Wikibooks
- char-pointer-function
- Says to change parameter to char** or char*&. I don't have that luxory.
- Pass By Reference Multidimensional Array With Unknown Size
Again, if I was writing this for myself, I would change the parameters (char** s
instead of char* s
for example), and use nice, useful functions like strcpy
, etc, but I cannot do that in this case. Any help/input would be very appreciated, thank you all so much in advance!