Solved

Declarint Char**

Posted on 2007-11-29
19
237 Views
Last Modified: 2010-08-05
Hello,

Could anybody help me with this declaration?
      char* xxx[30];
      xxx[0] = (char*)"test1";
        xxx[1] = (char*)"test2";

After this if I am trying to see the elements from the arrayalways the xxx[0] will be Bad pointer and all the information will start from xxx[1]

        xxx[0] = <Bad pointer>;
        xxx[1] = (char*)"test1";
        xxx[2]= (char*)"test2"    

I have tried with

      char ** x  = new char* [30];

      x[0] = new char[10];

In this case all I got was an empty string

Thank you very much
0
Comment
Question by:aureliuh
  • 7
  • 5
  • 3
  • +2
19 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20376497
Can you show the exact code you are using ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 20376529
Well, using

#include <iostream>
using namespace std;

int main () {

      char* xxx[30];
      xxx[0] = (char*)"test1";
      xxx[1] = (char*)"test2";

      cout << xxx[0] << endl;
      cout << xxx[1] << endl;

      return 0;
}

that works as expected...
0
 

Author Comment

by:aureliuh
ID: 20376535
Yes sure,


char* xxx[30];
 

xxx[0] = (char*)"command";
 

char * pch;

//str is a string that contain a command followed by parameters, separated by & sign

pch = strtok (str,"&");

while (pch != NULL)

{

xxx[counter] = pch;									

counter++;

pch = strtok (NULL, "&");

}

Open in new window

0
 

Author Comment

by:aureliuh
ID: 20376552
I know that shoul be ok. I don;t know what is happening. Could it be from a project setting or something?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20376559
This is not really consistent with the code you posted earlier. What is counter initialized to ? Can you also show the code where you notice the bad pointer problem ?
0
 
LVL 22

Expert Comment

by:grg99
ID: 20376561
If you want an array of 30 pointers, try:

typedef    char *           Str;

typedef    Str               pStr30         [30];


pStr30 xxx;


xxx[ 0 ] = "foo";
xxx[ 1 ] = "goo";






0
 
LVL 86

Expert Comment

by:jkr
ID: 20376564
The problem is

while (pch != NULL)
{
xxx[counter] = pch;   // <--------                                                                
counter++;
pch = strtok (NULL, "&");
}

'pch' is 'recycled' each time it is used, make that

while (pch != NULL)
{
xxx[counter] = strdup(pch);    // <---------
counter++;
pch = strtok (NULL, "&");
}

and it will work.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20376566
PS: don't fiorget to 'free()' the array entries afterwards.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20376576
>> 'pch' is 'recycled' each time it is used, make that

That's not how strtok works ...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 53

Expert Comment

by:Infinity08
ID: 20376588
But str might have gone out of scope by the time you try to access the xxx array.
0
 
LVL 4

Accepted Solution

by:
Fippy_Darkpaw earned 500 total points
ID: 20376599
Try this
// array of string pointers

char* xxx[20];

int i;
 

// initialize each pointer array 

for(i=0; i<20; i++)

{

	// create char array

	xxx[i] = new char[10];
 

	// populate array

	strcpy(xxx[i], "012345678");

}
 

// print array

for(i=0; i<20; i++)

{

	printf("%s\n", xxx[i]);

}
 

// delete arrays

for(i=0; i<20; i++)

{

	delete(xxx[i]);

}

Open in new window

0
 
LVL 4

Expert Comment

by:Fippy_Darkpaw
ID: 20376618
Does:

xxx[0] = (char*)"test1";

null terminate the string? That might be the problem. Try using:

strcpy strcpy(xxx[i], "test1");

istead
       
0
 
LVL 4

Expert Comment

by:Fippy_Darkpaw
ID: 20376631
oops. istead = instead hehe. =)
0
 

Author Comment

by:aureliuh
ID: 20376648
I have tryed all the methods that you suggested. I have attached the code


	char* xxx[30];
 

	int counter = 0;

	bool bFoundValue = false;
 

	CRegistry regMyReg;

	if (regMyReg.Open("Registry_Key that I want to open", HKEY_LOCAL_MACHINE)) {
 

		size_t nValueCount = regMyReg.Count();
 

		CRegEntry *reEntry;

		char str [MAX_PATH];

		for ( size_t n = 0; n < nValueCount; n++ ) {
 

			reEntry = regMyReg.GetAt(n);
 

			if (reEntry->IsString() && strstr(reEntry->lpszName, "Command") != NULL) {

				strcpy (str,*reEntry);

				reEntry->Delete();

				bFoundValue = true;

				break;

			}

		}

		if (bFoundValue){
 

			char * pch;

			pch = strtok (str,"&");

			while (pch != NULL)

			{

				xxx[counter] = strdup(pch);								

				counter++;

				pch = strtok (NULL, "&");

			}
 

			Function (xxx); -> here I saw that the first possition is BadPointer
 

			// Pass the xxx to another function

			

Open in new window

0
 

Author Comment

by:aureliuh
ID: 20376678
I saw the bad pointer setting up a breackpoint at the function line. I Go with the mouse over the xxx I can see the values from xxx
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20376683
You don't need to use strdup if you don't plan on using xxx when str goes out of scope.

Can you show the implementation of Function ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20376694
>> I saw the bad pointer setting up a breackpoint at the function line. I Go with the mouse over the xxx I can see the values from xxx

But what happens if you check it in the code ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20376702
btw :

>> Does:
>> 
>> xxx[0] = (char*)"test1";
>> 
>> null terminate the string?

"test1" is a string literal, and that is already null terminated, so yes.
0
 

Author Closing Comment

by:aureliuh
ID: 31411766
Thank you very much all of you. The problem was because of my IDE. What can I say. Anyway I consider that the Fippy_DarkPaw gives us a very good example of how to declare a char**. I have searched a lot before I have found out how to use it, so I think maybe there is someone how will need this answer. Thank you again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now