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

Pages: 1-

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:29

This isn't the board you are looking for.

Go here instead: http://7chan.org/halp

Name: Anonymous 2011-02-26 21:45

>>2
Yeah, we have to many incompetent fucks like this guy who's never written a line of c code in his life

Name: Anonymous 2011-02-26 21:53

>>3
So let's teach.  There'd be fewer around if we actually did a case by case assessment.

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.

Name: Anonymous 2011-02-26 22:10

Name: Anonymous 2011-02-26 22:14

>>5,6
YOU HELPED HIM!

Name: The guy who usually helps them 2011-02-26 22:19

>>8

fuck you faggot

Name: Anonymous 2011-02-26 22:28

>>9
You don't understand, do you? If you help them, they'll stay. They'll keep posting threads about the most basic programming questions and then they'll bring in their friends to do the same. And then this place will turn into /halp/. If you have such an urge to HELP THEM, just direct them to http://7chan.org/halp and help them there.

Name: Anonymous 2011-02-26 22:35

>>10
The content in this place hardly counts as decent quality as it is, so it shouldn't matter.

Name: Anonymous 2011-02-26 22:37

>>11
There's nothing so bad that it can't get worse.

Name: Anonymous 2011-02-26 22:42

>>12
True. But if look at quality logarithmic scale:
bad quality ====|===========|====================================== good quality
                |           |
                |         "help me" thread
        prog average thread


you'll see that help me threads actually improve quality of content on this board.

Name: Anonymous 2011-02-26 22:42


#include <stdlib.h>
#include <stdio.h>

void my_func(char **p, int len)
{
   int i;
   for(i = 0; i < len; i++)
   {
      *(p + i) = 'x';
   }
   return;
}

int main(int argc, char **argv)
{
   const int my_len = 100;
   char *p[my_len];
   int i;

   //Initialize to NULL.
//for(i = 0; i < my_len; i++) p[i] = NULL;

   //Allocate space for characters.
   for(i = 0; i < my_len; i++)
   {
      p[i] = malloc(sizeof(char));
      if(p[i] == NULL)
      {
    goto cleanup;
      }
   }

   //Set characters.
   my_func(p, my_len);

  //Print characters proving they were set.
  for(i = 0; i < my_len; i++)  printf("%c\n", *(p + i));

  cleanup:
  //left as an excersise to the reader
   return 0;
}

>>1

Name: Anonymous 2011-02-26 22:58

>>13
There are ``help me'' threads ``and help'' me threads. Having to explain to someone on /prog/ the difference between a string and an array of character pointers is kinda depressing.

Name: >>15 2011-02-26 23:00

s/``and help'' me/and ``help me''/

Name: Anonymous 2011-02-26 23:23

>>13
THIS IS WHAT YOU ACTUALLY BELIEVE

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