1

I got strucure

typedef struct 
{char *cells;}
 Map;

and cells suppose to be pointer to array of rows(in rows are integers on every position). I don't know how to access for example to number on 3. position in 2. row. I have stared with some array[3][3], but I don't know how to connect them with this struct. I tried

Map nextmap;
nextmap.cells[0] = array[0][0];

But I got only first number, which is clear. How can I get to other positions? Thanks in advance. EDIT: renaming the structure .. .

7
  • Note: You're dereferencing an indeterminate pointer, and thereby invoking undefined behaviour. Commented Sep 27, 2013 at 17:56
  • using new as the name for your structure is pretty iffy... Commented Sep 27, 2013 at 18:17
  • Why must you use char *cells; (as you indicated in a comment to an answer) instead of a different declaration for cells? Is the array to which cells will point an actual two-dimensional array (declared with something like char array[rows][columns];) or a pointer to pointers? Is the number of columns in the array fixed at compile time or stored in a variable? Commented Sep 27, 2013 at 19:08
  • because it's in a assigment in our school project :/ Commented Sep 27, 2013 at 19:10
  • @JakubFedor: Please answer the remaining two questions. Commented Sep 27, 2013 at 20:05

3 Answers 3

1

When you did Map nextmap;, you created an uninitialized Map struct. When you did nextmap.cells[0] = array[0][0]; you dereferenced (i.e. followed) the uninitialized pointer, and stored a value at the random memory it points at.

If you want to initialize the cells structure, you can do something as simple as nextmap.cells = array[0]; That will cause nextmap.cells to point at array. Note that it's not copying the contents; just pointing at them. That means that if you change the values through cells, you'll be modifying the values in arrays.

(Also, using 'new' as a variable name is perfectly acceptable in C, but you're likely to confuse any C++ programmers reading your code, since 'new' is an operator in that language.)

new now changed to nextmap in question

Edited to correct the type mismatch in nextmap.cells assignment.

Sign up to request clarification or add additional context in comments.

5 Comments

new.cells = array will cause problems because the type of new.cells is not appropriate for a two-dimensional array.
Ye, I know, but I have to use one dimensional array to point to array of rows. EDIT: assigment from incompatible pointer type EDIT2: I have to use this struct
Either the declaration of cells should be changed to match the type of array or (less desired) a cast should be inserted into the assignment.
If you want to point to the row, you can do new.cells = array[0];, assuming that array is a char[][]. The type of array[0] would then be a char[], which automatically decays to char*.
After you've assigned arrays[0] to new.cells, the first item in the row will be new.cells[0]. The second item will be new.cells[1].
1

Given an array char array[][NumberOfColumns] (the first dimension is irrelevant and is omitted here; it would be needed when the array is defined), you can set a pointer to the first element of the array with:

nextmap.cells = &array[0][0];

Then you can access an element in the array, array[i][j], by calculating its position within the array, with either of these two expressions:

*(nextmap.cells + i*NumberOfColumns + j)
nextmap.cells[i*NumberOfColumns + j]

Two-dimensional arrays generally ought to be addressed as two-dimensional arrays. Calculating the position manually is poor practice if done without good reason. If this school assignment did not have a good reason for this, then it is a bad assignment.

Comments

0

First of all new is not a good name for a variable.

new now changed to nextmap in question

Second of all in your case cells should be a double pointer, like this

char ** cells;

Or a pointer to a 2D array, like

char (*cells)[N][N];

where N is a constant you want to use.

1 Comment

char (*cells)[N][N]; is an inappropriate declaration if cells is to be assigned with cells = array;, where array is declared as char array[N][N];. If you keep the current declaration, cells would have to be assigned with cells = &array;, and elements would have to be accessed with three dereferences, as (*cells)[i][j].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.