473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Use of char **

Hi everybody,
I'd like to ask for help with usage of char **. I have a following func, which dynamically allocates pointer a array

and pointers of char[] arrays in it:

Expand|Select|Wrap|Line Numbers
  1. int sptr(char **ppchr)
  2. {
  3.   int x = 0, ret = 0;
  4.   char *pchr = NULL, **ptchr = NULL;
  5.  
  6.   // In place of for() you can imagine enumeration if something, quantity
  7.   // of which is not known during design time. It's in extra func because
  8.   // I'll need recursion (then I'll need extra parameter long *  for index
  9.   // forward of course)
  10.   for(x = 0; x < 10;x++)
  11.   {
  12.     // array of pointers allocation
  13.     if((ptchr = (char **)realloc(ppchr, sizeof(char *) * (x + 1))) == NULL)
  14.     {
  15.       printf("ERR realloc\n");
  16.       ret = 1;
  17.       goto CleanExit;
  18.     }
  19.     ppchr = ptchr;
  20.     // char[] allocation
  21.     if((pchr = (char *)malloc(sizeof(char) * (11))) == NULL)
  22.     {
  23.       printf("ERR malloc\n");
  24.       ret = 2;
  25.       goto CleanExit;
  26.     }
  27.     memset(pchr, '\x0', sizeof(pchr));
  28.     ppchr[x] = pchr;
  29.     // fill out chararray
  30.     sprintf(ppchr[x], "Item%d", x);
  31.   }
  32.  
  33. CleanExit:
  34.   // freeing is only done if an error occurs, otherwise it manages
  35.   // 'upper' procedure after it processes contents
  36.   if(ret)
  37.   {
  38.     for(x = 0; x < 10;x++)
  39.       if(ppchr[x])
  40.       {
  41.         free(ppchr[x]);
  42.       }
  43.     free(ppchr);
  44.   }
  45.   return ret;
  46. }
And now where my problem is: as far as everything including contents processing is in one single procedure, everything goes like a charm, but I need from sptr() allocations and fill out and then to give that array to some func, which needs char ** as a parameter.

So I moved declaration of **ppchr one level upper:

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char* argv[])
  2. {
  3.   char **ppchr = NULL;
  4.   int x = 0, ret = 0;
  5.  
  6.   ret = sptr(ppchr);
  7.  
  8.   printf("sptr ret: %d\n", ret);
  9.   for(x = 0; x < 10;x++)
  10.   {
  11.     printf("Contents %d=%s\n", x, ppchr[x]);
  12.   }
  13.  
  14.   if(!ret)
  15.   {
  16.     for(x = 0; x < 10;x++)
  17.     if(ppchr[x])
  18.     {
  19.       free(ppchr[x]);
  20.     }
  21.     if(ppchr)
  22.       free(ppchr);
  23.   }
  24.   return 0;
  25. }
  26.  
.. and sudenly everything goes wrong :-(. Or I have mistake in that declaration or in way of passing the array to sptr(), but I just never get that array back to main(), it almost looks like I'm passing this way something absolutely different :-). Notice that number nor size of char items is not known before run time and that I really need to pass it later as char **. So please don't advise use of struct, which is way I'd normally go in this case.

Please explain what's wrong or how to make it work. I though that another way is not to pass initial char** pointer to sptr() and to return it instead (char **sptr(char *some_par()), however I just don't understand why the hell this doesn't work :-(

Please don't advise vectors nor CString etc. I want to make it work in C, because I want to UNDERSTAND it, which usage of vectors doesn't give me.

Thanks a lof for any help.

Mates
Jan 26 '09 #1
2 2079
newb16
687 512MB
Add third '*' , like char *** ppchr; ... (*ppchr) = (char*) realloc( *ppchr, ... );
and in main program call it like ... ( &ppchr)
Jan 27 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
The argument ppchar is a COPY of the char** in main(). Any change to ppchar is a change to the COPY. The char** in main() is never altered.

To do do this you need the address of the char** in main(). That means
sptr() needs a char*** argument.

What I would do is have sptr() return a void* and then typecast that void * into the correct pointer type.
Jan 27 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.