June 25, 2012

 

Using 2D static char array in a function (C programming)

Obviously, there are a number of different ways to do this - but this seems to me to be the most simple, basic way to do this.

Say that in your main() you've set up a 2D char array (or you can think of it as an array of "strings"), like so (note that I'm using the gcc compiler, with C99 option):


  const unsigned int sz_addr = 50 + 1;
  const unsigned int sz_faddr = 10;
  char faddr[sz_faddr][sz_addr];

  // initialize
  for (unsigned char i=0;i<sz_faddr;++i) { strcpy(faddr[i],""); }

Now, using the array right there in main() is straightforward:


  // put some data in the array
  strcpy(faddr[0],"one");
  strcpy(faddr[1],"two");
  strcpy(faddr[2],"three");

But let's say you want to pass the 2D char array to a function (along with some other pieces of information perhaps), then do whatever it is you're doing and filling the 2D char array in the function based on your analysis. (In my recent application I'm working on, I'm actually passing two different 2D char arrays to a function, then analyzing the information in the first set of "strings" and manipulating them and putting the results in the second set of "strings".)

The point here (no pun intended) is that we're using a static 2D char array, not a char** variable that has had memory dynamically allocated to it, so you can't just directly pass pointers through the function, since the variable, "faddr" in this example, is not pointers, but a name of a static 2D array. But just saying this this way gives us the simple answer: Just make a pointer reference to the static 2D array, fill it in with the appropriate memory addresses (of where the "strings" are located), then pass the pointer variable to a function. (There is absolutely nothing of any real usefulness in this particular example function; this is merely to demonstrate the basic function parameter method.)

Like so:


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

void testpp(char**,unsigned int,unsigned int);

int main(void) {
  const unsigned int sz_addr = 50 + 1;
  const unsigned int sz_faddr = 10;
  char faddr[sz_faddr][sz_addr];

  // initialize
  for (unsigned char i=0;i<sz_faddr;++i) { strcpy(faddr[i],""); }

  // enter some data (first 5 "strings" only)
  strcpy(faddr[0],"one");
  strcpy(faddr[1],"two");
  strcpy(faddr[2],"three");
  strcpy(faddr[3],"four");
  strcpy(faddr[4],"five");

  // define and allocate a pointer to a bunch of char pointers
  char** pfaddr = malloc(sz_faddr*sizeof(char*));

  // initialize pointer with addresses of the beginning of each
  // "string" of the static 2D char array
  for (unsigned int i=0;i<sz_faddr;++i) { *(pfaddr+i) = &faddr[i][0]; }

  // pass the pointer to the function
  testpp(pfaddr,sz_faddr,sz_addr);

  // print "strings" to stdout (first 5 only)
  printf("%s\n",faddr[0]);
  printf("%s\n",faddr[1]);
  printf("%s\n",faddr[2]);
  printf("%s\n",faddr[3]);
  printf("%s\n",faddr[4]);

  free(pfaddr); // deallocation
  return(0);
}

void testpp(char** px,unsigned int m,unsigned int n) {
  strcpy(*(px+0),"eleven");
  strcpy(*(px+1),"twelve");
  strcpy(*(px+2),"thirteen");
  return;
}

Please note the following: Even though I've defined the function, as shown here, to include the two sizes information ("m" and "n"), I've written only very basic code and am not actually using the information at all, merely using the strcpy() function inside the testpp() function, so this certainly is not security-safe code in any way (even though I'm showing the "m" and "n" for such facility). This example merely shows a method for how to use a static 2D char array and working with that in a function through the intermediary of an array of pointers to the "strings" of the array. In a "production" application with variables holding data coming from some user input (say, from an input data file), you'd obviously want to be checking lengths and truncating at the array maximum lengths (allowing one char for the null-terminator).


This page is powered by Blogger. Isn't yours?