char **pointers

How to assign chars to the pointer array? Would it be:
1
2
3
4
5
6
7
8
9
 char somefunction (char **array) 
// char 1darray
// detect  length
// Outer loop to control the inner dimension
for (counter = 0; counter < length; counter ++)
{
   array [outerloopvalue][counter] = 1darray[counter] 
}
cout << array [outerloopvalue]

Sorry for laziness but, I think it is straight forward. However, would that array[][] = 1darray [] statement work? meaning would it be stored? Or I am tackling this problem wrong?
i can not see any problem with this code.
does it give you the expected output?
I get no output at all which is the problem. Which to me suggests that it is assign blanks or something of that nature. I'll say that the char** array is being passed from a different source file which was created by function if that makes sense and if that makes any difference. (essential the function creates char array [size][] and points to it.) However, I thought that wouldn't matter assuming the function prototype in the header is correct and the pointer in question is indeed a char **array.

closed account (18hRX9L8)
Example code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdio>

typedef char Board[3][3]; // defining type Board for ease of use
typedef struct POINT { // simple point for ease of use
  long x;
  long y;
} POINT;

POINT CreatePoint(int xcoord, int ycoord) { // simple point for ease of use
	POINT pt;
	pt.x = xcoord;
	pt.y = ycoord;
	return pt;
}

int AssignToBoard(Board bdassign, char chassign, POINT ptassign) { // assigns the char to a point on the board
	try {
		bdassign[ptassign.y][ptassign.x] = chassign; // since the board is a pointer, we dont have to worry about nothing happening
	} catch(int e) {
		return e;
	}
	return 0;
}

void DisplayBoard(Board bd) { // displays the board
	printf(" %c | %c | %c \n", bd[0][0], bd[0][1], bd[0][2]);
	printf("---+---+---\n");
	printf(" %c | %c | %c \n", bd[1][0], bd[1][1], bd[1][2]);
	printf("---+---+---\n");
	printf(" %c | %c | %c \n", bd[2][0], bd[2][1], bd[2][2]);
}

int main(void) { // now that we defined all our nice and easy functions, our main isnt going to be that long ;)
	Board b; // define a board (char[3][3])
	
	for(int y=0; y<3; y++) {
		for(int x=0; x<3; x++) {
			AssignToBoard(b, 'X', CreatePoint(x, y)); // initialize all the points as a test
		}
	}
	
	DisplayBoard(b); // print the board to see if it worked
	
	getchar(); // keep program running
	return 0;
}
Last edited on
I don't understand the relevance or intention of the above post?
closed account (18hRX9L8)
kingkong200 wrote:
How to assign chars to the pointer array?

You wanted to know how to assign chars to an array. I posted some example code as a reference.
Sorry if I was bit too ambiguous, but I was to figure out why the pointer I set up wasn't assigning the 1dchar array to the 2d char array which is the pointer. That why I asked how to assign chars to the pointer array or specifically the array I set up in case I was way off track. For if I understand your program right it is storing the index position in the struct and then saying at these index position that place an 'X'?. However, wouldn't my for loop have the same effect? but takes in the account of a 1d char array?
Last edited on
closed account (18hRX9L8)
Yes it should work as long as the 1darray and the row of the 2darray are the same length and you initialized the 1darray.

You can test if your 1darray is initialized by running this statement:

1
2
3
for (int x=0;x<length;x++) {
        std::cout << (int)1darray[x] << " ";
}


If you get a bunch of 0s (NULLs), that means you have not initialized 1darray.
Last edited on
i'm sorry, i should've seen the first error before:
ISO/C++ standard specifies:
2.11 Identifiers
. . .
nondigit: one of
a b c d e f g h i j k l m
n o p q r s t u v w x y z
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z _
digit: one of
0 1 2 3 4 5 6 7 8 9
. . .
The initial element shall not be a universal-character-name designating a character whose encoding
falls into one of the ranges specified in E.2.
. . .

i believe E.2. is the range named digit:
in other words, a variable's name can't start with a number (i'm referring to 1darray).

when i copied the code you posted, and implemented it into a simple program, i had a problem:
i couldn't pass a 2D array to the function, the compiler said:
error C2664: 'somefunction' : cannot convert parameter 1 from 'char [2][13]' to 'char **'

i think you should change the argument of somefunction() to be of type char [][], i don't know why however.

one last thing, when i copied your code my VS2010 printed the identifier array in blue font, normally when this is the case it means that the IDE recognises this word to be of special meaning so i advise you to avoid using this name as an identifier.

PS: an identifier is the name of a (variable, function, object, class,....etc), basically anything that has a name, you can call its name an identifier.
I changed the names of the pointer name. However, in terms of the dimensions. Essentially the pointer
1
2
3
 **storearray = storearray[value][]
// so in other words the outer dimension isn't defined/created.
 


So does that outer dimension need to be defined prior to placement of the 1darray?
and if that is the case would the program stable if I were to change the outer dimension according to the 1d array length? as there are multiple words to be placed.
1
2
3
4
5
6
7
8
9
10
// so in some bad psudecode
char somefunction (**storearray) {
//detect string word 
// detect length 
// storearray [indexnumber][length]
// create loop 
// place chars into storearray
cout << storearray [indexnumber] 
}
// repeat process with new word 

Or would you have to set the outer dimension to a const int? and then delete excess elements somehow?
.
Last edited on
When I ran like this created segmentation fault.



Bumping myself....
closed account (18hRX9L8)
Yes, you have to define it. So when you are defining the full array, add 1 to the size. For example:

Before:
char x[SIZE][];

After:
char x[SIZE+1][];



[EDIT]
If you want to add an extra line, you need to define a new char** that is bigger than the first and move all the values in the old array to the new one.
[/EDIT]
Last edited on
I see thanks.
Topic archived. No new replies allowed.