I'm new to c and attempting socket programming. I have a question about pointers.
Say I have a setup like this:
int main() {
... // some code
int numbytes = receive(i);
}
int receive(int num) {
... // some code
msgLength = ... // some code
char msgWithLength[4 + msgLength];
int n = getMsg(clientSocket, msgWithLength, 4 + msgLength);
char * msg = unpack(msgWithLength, 4);
return n;
}
char * unpack(char *msgWithHeader, int headerSize) {
char *msg = ... // some code to remove header from msgWithHeader
return msg;
}
What I want is to access msg in in main. I don't want recieve to return msg though, I want it to return an int. Also, in main, I don't know the size of msg.
Ideally I'd have something like
int main() {
char * msg;
int numbytes = receive(i, msg);
// And be able to access msg here
}
But this doesn't work. I guess my question is related to pointers and passing by reference. Any ideas on how to get this to work?