Original Post
🔒 Locked
problem assigning dynamic memory
Advertisement
p = new int[10 * 10];
When you are doing dynamically-allocated arrays this way, compiler won't care how you address the subscripts.
int test = p[2, 3];
will work and so will
int test = p[3, 4, 1];
edit - I guess I should mention that by "work" I mean the compiler won't complain. But you'll have to implement your own subscripting offset logic for rows and columns in the 2D array.
When you are doing dynamically-allocated arrays this way, compiler won't care how you address the subscripts.
int test = p[2, 3];
will work and so will
int test = p[3, 4, 1];
edit - I guess I should mention that by "work" I mean the compiler won't complain. But you'll have to implement your own subscripting offset logic for rows and columns in the 2D array.
Thx for your help but now im haveing another problem.
When im running this:
with this being in the something.mp:
5 4
3 42 4 12 57
34 87 124 568 2
32 57 234 8 2
83 672 76 78 5
when i run the following its show the last 5 characters 4 times.
How would is solve this?
Thx Fyber
When im running this:
#include <stdlib.h>#include <stdio.h>#include <iostream>#include <fstream>int * pMap;int x,y;FILE * pFile;int ReadMap(char * filename){ pFile = fopen(filename, "rt"); fscanf(pFile,"%d %d\n", &x, &y); pMap = new int[x,y]; int z; for (int j=0; j < y; j++) { for (int i=0; i < x; i++) { fscanf(pFile, "%d ", &z); pMap[j,i] = z; } fscanf(pFile, "\n"); } fclose(pFile);}int main(){ ReadMap("something.mp"); cout << x << " " << y << endl; for (int j=0; j < y; j++) { for (int i=0; i < x; i++) { cout << pMap[j,i] << ", "; } cout << endl; } delete [] pMap; system("PAUSE"); return 0;}with this being in the something.mp:
5 4
3 42 4 12 57
34 87 124 568 2
32 57 234 8 2
83 672 76 78 5
when i run the following its show the last 5 characters 4 times.
How would is solve this?
Thx Fyber
pMap[j,i] = z;This is not a multi-dimensional array access. You use the comma operator, which returns the result of the expression after the last comma, i.e. i.
You probably wanted to write
pMap[j][j] = z;
Quote:
Original post by dalep
p = new int[10 * 10];
When you are doing dynamically-allocated arrays this way, compiler won't care how you address the subscripts.
int test = p[2, 3];
will work and so will
int test = p[3, 4, 1];
edit - I guess I should mention that by "work" I mean the compiler won't complain. But you'll have to implement your own subscripting offset logic for rows and columns in the 2D array.
Not in C++. That invokes the seldom-used "comma operator", such that e.g. for p[2, 3], the effect is "evaluate '2', throw away the result, then evaluate '3'; then the result of evaluating '3' (i.e., 3) is used for subscripting". You need to calculate the offsets manually, in place, because there is nowhere you could "pass" the two arguments to - even if you made a custom struct or class, operator[] overloads can only accept one argument, period.
You should probably read this, and then forget about doing it yourself and use this.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement