C: pointers, strings, splitting headache
Okay, I am totally new to C and I think my main problem is that I can see how to solve my problems SO easily in Perl, but I am just getting confused on things.
Like, for instance, I am just trying something simple here and trying to send a string to a function, have that string split on a delimter, then return a string telling me the parts. Below is one of 20-25 different ways I've tried things. Looking at it for me, it seems it should work. It does compile, but all I get is a segfault. I know I must be way off, but either I am just looking at this the wrong way or I'm just an idiot. Code:
#include <stdio.h> |
dieResults is declared in the function - so the memory for it is allocated when the function starts and deallocated when function exists
solutions: 1. declare it with 'static' prefix 2. declare it as global variable, outside of any function (including main function) 3. declare it inside main() and pass as argument to modified rollDice function |
I can't create and return its value from rollDice?
How do I create functions to simply return strings, then? Do I have to create every string variable globally? Seems kind of odd. Like, what if I just want to have a function that returns a string, that's it (psuedo-code below): main () { char mainStr[] = sayYay(); } sayYay { char myStr[] = "Yay!"; return myStr[]; } Is the above not possible without a global variable? |
the problem is C doest really have strings as you understand them, it just has arrays which are essentially pointers. when you return dieResults, you are reurning a pointer to where your array(string) is stored but because it is deleted when the function exits, the pointer now points to something completely random and is known as a wild pointer.
you can never return a pointer to a local variable(which is what your doing) because the variable will not exist once the function is left. the best(IMHO) solution as dorian33 suggests is to declare it in main and pass a pointer to it. making the variable static is a nice hack but for someone else reading the code it might be confusing, and i wouldnt use that technique in an arbitrary function for returning a string. i wouldnt declare the value as a global variable because it will just pollute the namespace without good cause. if you dont mind trying c++ then the std::string class is just what your after. example passing pointer to function: void sayHi(char *y) { sprintf(y, "Hello\n"); } int main() { char x[50]; sayHi(x); printf("%s", x); return 0; } |
C is more like assembly/machine-code then perl actually, though the syntax may look more like perl then assembly.
When you have: Code:
char str[] = "Aloha"; If you declare a local variable "char str[10];" then the array of 10 char's and the 4 bytes of the pointer are stored on the stack and they are disposed of when the funtion returns. Now if you return str then you actually return the 4 bytes containing an address where the string was (note past tense) stored while your function was running. If you don't want to declare the string to be 'static' or globally you can declare the string in the main function, pass the pointer as an argument to the function, and fill it with char's in your function. You cannot change a variable belonging to main() in the function by passing it as an argument, because that will be only a local copy of it. The good news is that you don't need to change it, because it is an address. But because the adsress now is known by the function, the function can write a string to the memory space the pointer points to. So the function writes the string directly into the memory that belongs to main(), and now there's no need to even return something. Code:
#include <stdio.h> Code:
#include <stdio.h> |
Thanks for all the help. I was able to get what I wanted, so now I am moving on. I can read and read all the C material I want (like the K&R book I am reading now), but I want to try and apply as I go along, otherwise it's not going to click for me.
If anyone is curious, for now I am just trying to roughly translate this Perl sub into C: Code:
sub RollDice { |
All times are GMT -5. The time now is 01:13 AM. |