473,386 Members | 1,812 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.

Char***

115 100+
Im having trouble in the c++ world grasping char arrays.

Im given a char*** from a thrid party function and i want to loop through the returned strings and just message box them.

This is what i have:

Expand|Select|Wrap|Line Numbers
  1.     char ***lst;
  2.     ll_cnt = GetWords(lst);
  3.  
  4.     for(int i = 0; i < ll_cnt; i++)
  5.     {
  6.         MessageBox(0, str, *(lst[i]), 0);
  7.     }
It works the first time, where i = 0, but after that fails.

What am i doing wrong?
May 6 '09 #1
5 1698
donbock
2,426 Expert 2GB
Let's clarify that char*** variable. Do you have something similar to this:
Expand|Select|Wrap|Line Numbers
  1. char a[] = "some string";
  2. char *b = &a[0];
  3. char **c = &b;
  4. char ***d = &c;
or is it more like this:
Expand|Select|Wrap|Line Numbers
  1. char a[] = "some string";
  2. char *b[] = { &a[0], ... };
  3. char **c[] = [ &b[0], ... };
  4. char ***d = &c[0];
You need to know the memory organization, not just the variable types.
May 6 '09 #2
ShadowLocke
115 100+
Well i cant really figure it out..

The function I call is defined as "int GetWords(char*** slst);"

Though i just figured out my problem (blindly troubleshooting and playing)

I changed:

Expand|Select|Wrap|Line Numbers
  1. *(lst[i])
to:

Expand|Select|Wrap|Line Numbers
  1. (*lst)[i]
and it worked!

Thanks!
May 6 '09 #3
donbock
2,426 Expert 2GB
Sounds like the memory organization is something like this ...

Expand|Select|Wrap|Line Numbers
  1. #define N <number of words>
  2. int ll_cnt = N;
  3. char *array[N] = { "string1", "string2", ..., "stringN" };
  4. char **pArray = array;
  5. char ***lst = &pArray;
(*lst) is the value of pArray, which is the address of the array; which you index into with [i]. The result is successive char-pointers from the array.
May 6 '09 #4
weaknessforcats
9,208 Expert Mod 8TB
Please read this article: http://bytes.com/topic/c/insights/77...rrays-revealed.
May 6 '09 #5
Banfa
9,065 Expert Mod 8TB
@ShadowLocke
This looks wrong to me.

If you have a function that takes a char *** parameter and uses it to pass back data then that function is expecting a pointer to an existing location but you do not pass a pointer to an existing location you pass the value of a pointer.

The only place GetWords has to write to in your function is the completely random and uninitialised address you pass in contained in lst.

Also from the name and return type it appears that the function is returning a list of words. Dynamically allocated that would be an array of pointers with each pointer pointing to allocated memory. A pointer to such an array would have type char **.

Taking all this in mind I think the 2 code lines above should actually be

Expand|Select|Wrap|Line Numbers
  1.     char **lst;
  2.     ll_cnt = GetWords(&lst);
  3.  
passing the address of a char ** for the function to write the address of the array of strings to. Obviously the change of type would then also have an effect on the rest of your code.

At the moment I believe the 2 lines highlighted actually produce undefined behaviour as you are writing to an un-allocated memory address somewhere.

When a function takes a pointer then the majority of the time it wants the address of an allocated piece of memory or may be NULL, not an uninitialised pointer.
May 7 '09 #6

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.