Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C Question

Name: Anonymous 2011-02-26 21:22

How do i pass a character pointer to an array into another function so that function can manipulate it?
I have a c program with everything in the main method and i want to simplify it, but i cant successfully pass a char *[] to another function.

helper(char *p);

int main(){
 char *p[100];
 helper(p);
 
 return 0;
}


void helper(char *p){
 //do stuff with p[0],p[1], etc
}

Name: Anonymous 2011-02-26 21:56

but i cant successfully pass a char *[] to another function
That's because your function expects an array of characters, not an array of pointers to chars.

Name: Anonymous 2011-02-26 22:03

>>1
So here you have declared an array of 100 character pointers, yes? They don't point to anything; indeterminate garbage. You then want to pass this array of pointers to a function, presumably so you can manipulate the character's the pointer's point to?
char *p[100];

An array is already a pointer. So p is a pointer to a pointer that points to a char. Ok? So p is pointer to the first character pointer.

void func(char *p)
{
   *(p + 0) = 'x'; #Store x at the location pointed to by p.
   *(p + 1) = 'y'; #Store y at the location pointed to by
                   #1 element from the first (the second pointer
                   #).
   #. . .
}

It might be helpful to do something like:
const int my_len = 100;
char *p[my_len];
and then pass my_len to your function as well to easily know how many pointers there are.

I have only proved this correct not tested it.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List