New to C and this site. I am trying to randomly select a name in an array of arrays in a function. Then return that random name to main where I can use it:
#include <stdio.h>
#include <time.h> // For rand function
int random_number(int, int);
char * random_name(char *);
int main(void)
{
char * main_pointer;
printf("\nIn main:\nmain_pointer = %s\n", main_pointer);
return 0;
}
char * random_name(char * MAIN_POINTER)
{
int x = random_number(0,7);
char random[7][5] =
{"0Sam", "1Sam", "3Sam", "4Sam", "5Sam", "6Sam", "7Sam"};
MAIN_POINTER = &random[x][0];
printf("In the function:\nrandom = %s\nMAIN_POINTER = %s\n", (&random[x][0]), MAIN_POINTER);
return MAIN_POINTER;
}
int random_number(int min, int max)
{
int roll;
int maximum = max - min;
srand(time(NULL));
roll = (rand() % maximum) + min;
return roll;
}
Sample run:
In the function:
random = 0Sam
MAIN_POINTER = 0Sam
In main:
main_pointer = Ø'Þ¿¦¼i·
As you can see it becomes garbage.
There are several problems with the code as posted, but lets start with this:
char * main_pointer;
This is always a bad thing to do, it leaves "main_pointer" as uninitialized. Your compiler should have told you this.
test.c: In function ‘main’: test.c:12:7: warning: ‘main_pointer’ is used uninitialized in this function [-Wuninitialized]
If you're using GCC, then add the "-Wall" compiler option to get more diagnostic output, and always try to assign a default value to your variables, e.g.
char * main_pointer = NULL;
Next problem:
char * random_name(char * MAIN_POINTER)
....
MAIN_POINTER = &random[x][0];
While this is legal, I don't think it does what you are expecting. When you pass pointers as function arguments like this, the address the pointer is pointing to is passed and is stored in a new pointer variable. Any changes made are made to this new copy and when the function returns, they are lost.
char* test(char* in)
{
in = in + 1;
return in + 10;
}
....
char* p = (char*)1000; // 'p' now points to memory location 1000.
char* q = test(p);
printf("p = %p, q = %q\n", p, q);
Will print:
1000 1011
Essentially, pointers work just like variables, in this case "in" was initialized to the same value as "p", the address "1000", when we added 1 to it, it was only the private variable "in" that was modified.
The only special feature of a pointer is that you can "dereference" it. If you wanted your "random_name" function to be able to alter the pointer main_pointer, you would need to pass the address of the pointer and dereference that:
void random_name(char** main_pointer) // address of the pointer
{
...
(*main_pointer) = random[x];
}
Since the call to random_name is missing from your code, I can't tell if you were expecting to receive it like this:
main_pointer = random_name(main_pointer);
Doesn't seem like a lot of point passing it to the function in this case since you never actually use it.
Another problem your code is going to run into is that you are trying to use a pointer to hoist data from inside the scope of a function to something above it on the stack. This is dangerous. If you need data to persist or be visible outside of a function, it needs to either be declared in the global scope or given the "static" attribute.
const char* random_names[] = { "0Sam", "1Sam", "2Sam", ... };
// or
void random_name(const char** main_pointer)
{
size_t x = random(0, 7);
static const char* random_names[] = { "0Sam", "1Sam", "2Sam", ... };
(*main_pointer) = random_names[x];
}
You never set the value for main_pointer in main() function, it just has a garbage address.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With