let's say i have a 2D array, how would i access it with a pointer?
int x [2][2];
func(x);
void func(int *x) {
//i want to fill it with a number, like 1
} 14 6169
Dmitrii PapaGeorgio wrote: let's say i have a 2D array, how would i access it with a pointer?
int x [2][2];
func(x);
void func(int *x) { //i want to fill it with a number, like 1 }
forgot...the array is being passed as func(x[0])
Dmitrii PapaGeorgio wrote: let's say i have a 2D array, how would i access it with a pointer?
int x [2][2];
func(x);
void func(int *x) { //i want to fill it with a number, like 1 }
The above array looks like this:
+---+---+---+---+
|0,0|0,1|1,0|1,1|
+---+---+---+---+
Each box represent an integer. The indices are n,m as: x[n][m].
--
WW aka Attila
inside the function when i try to assign a value i get an error saying
it's not a pointer or array like
x[1][2] = 1;
WW wrote: Dmitrii PapaGeorgio wrote:
let's say i have a 2D array, how would i access it with a pointer?
int x [2][2];
func(x);
void func(int *x) { //i want to fill it with a number, like 1 }
The above array looks like this:
+---+---+---+---+ |0,0|0,1|1,0|1,1| +---+---+---+---+
Each box represent an integer. The indices are n,m as: x[n][m].
"Dmitrii PapaGeorgio" <vr****@woh.rr.comNOSPAM> wrote in message
news:zw*********************@twister.neo.rr.com... let's say i have a 2D array, how would i access it with a pointer?
int x [2][2];
func(x);
void func(int *x) {
That type will only work for a single dimension array.
//i want to fill it with a number, like 1 }
#include <cstdlib>
#include <iostream>
void func(int (*x)[2])
{
x[0][0] = 1;
x[0][1] = 1;
x[1][0] = 1;
x[1][1] = 1;
}
int main()
{
int x [2][2] = {0};
func(x);
for(size_t i = 0; i < sizeof x / sizeof *x; ++i)
for(size_t j = 0; j < sizeof *x / sizeof **x; ++j)
std::cout << "x[" << i << "][" << j << "] == "
<< x[i][j] << '\n';
return 0;
}
Why are you using an array instead of a container?
-Mike
>let's say i have a 2D array, how would i access it with a pointer? int x [2][2];
func(x);
void func(int *x) { //i want to fill it with a number, like 1 }
The same as an int :
int x = 5;
f(x);
// takes an int
void f(int i)
{
i = 2;
}
The reason for which 'i' is of type 'int' is because 'x' is of type
int.
Now if 'x' is of type 'int[2][2]', well 'i' will be too :
void f(int i[2][2])
{
i[0][0] = 4;
}
But if you allocated the array with new, you probably did something
like
int size_1 = 2;
int size_2 = 2;
int **x = new int*[size_1]
for ( int i = 0; i<size_1; ++i)
x[i] = new int[size_2];
Since 'x' is now of type 'int**', that is what 'i' will be :
void f(int **i)
{
i[0][0] = 4;
}
The 'problem' with that is now you have no idea about the bounds of
'i'. You have two solutions. Either you pass the information too :
void f(int **i, int size_1, int size_2)
{
}
or you use something like the zero-terminated c-style string and
always set the last element to a given value which means end-of-array.
But know that arrays are always passed by address, that is, modifying
its value in f() will modify 'x' too.
Jonathan
i'm using array and not a container since my teacher is a fruit cake.
thanks-
Mike Wahler wrote: "Dmitrii PapaGeorgio" <vr****@woh.rr.comNOSPAM> wrote in message news:zw*********************@twister.neo.rr.com...
let's say i have a 2D array, how would i access it with a pointer?
int x [2][2];
func(x);
void func(int *x) {
That type will only work for a single dimension array.
//i want to fill it with a number, like 1 }
#include <cstdlib> #include <iostream>
void func(int (*x)[2]) { x[0][0] = 1; x[0][1] = 1; x[1][0] = 1; x[1][1] = 1; }
int main() { int x [2][2] = {0}; func(x);
for(size_t i = 0; i < sizeof x / sizeof *x; ++i) for(size_t j = 0; j < sizeof *x / sizeof **x; ++j) std::cout << "x[" << i << "][" << j << "] == " << x[i][j] << '\n';
return 0; }
Why are you using an array instead of a container?
-Mike
Dmitrii PapaGeorgio wrote: inside the function when i try to assign a value i get an error saying it's not a pointer or array like x[1][2] = 1;
Because inside the function you have no array, you have a pointer to the
first element of the array:
+-+ +---+---+---+---+
|x|-->|0,0|0,1|1,0|1,1|
+-+ +---+---+---+---+
0 1 2 3
Each box represent an integer. The indices inside the boxes are n,m as:
x[n][m].
--
WW aka Attila
Dmitrii PapaGeorgio wrote: i'm using array and not a container since my teacher is a fruit cake. thanks-
[SNIP]
Fruitcakes top post. Your teacher wants you to learn arrays.
--
WW aka Attila
Jonathan Mcdougall wrote: The same as an int :
int x = 5; f(x);
// takes an int void f(int i) { i = 2; }
The reason for which 'i' is of type 'int' is because 'x' is of type int.
Now if 'x' is of type 'int[2][2]', well 'i' will be too :
Actually, in nearly all contexts 'x' will be of type int (*)[2] (pointer
to array of two ints). Array names usually become pointers to the type
of the objects stored in the array. There are exceptions to this, for
example when the name is used as the operand of the sizeof or unary &
operators. void f(int i[2][2])
Equivalent to
void f(int (*i)[2])
{ i[0][0] = 4; }
But if you allocated the array with new, you probably did something like
int size_1 = 2; int size_2 = 2;
int **x = new int*[size_1]
for ( int i = 0; i<size_1; ++i) x[i] = new int[size_2];
Since 'x' is now of type 'int**', that is what 'i' will be :
void f(int **i) { i[0][0] = 4; }
The 'problem' with that is now you have no idea about the bounds of 'i'.
This isn't a new problem. You had the same problem before:
void f(int i[2][2])
{
//...
}
This doesn't give the size of the array i (which isn't actually an
array), it just looks like it does. That first '2' is absolutely useless
- the compiler ignores it.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
"Dmitrii PapaGeorgio" <vr****@woh.rr.comNOSPAM> wrote in message
news:cz*********************@twister.neo.rr.com... Dmitrii PapaGeorgio wrote: let's say i have a 2D array, how would i access it with a pointer?
int x [2][2];
func(x);
void func(int *x) { //i want to fill it with a number, like 1 }
forgot...the array is being passed as func(x[0])
That means that a pointer to x[0][0] is being passed.
#include <iostream>
void func(int *x)
{
x[0] = 1;
x[1] = 1;
x[2] = 1;
x[3] = 1;
}
int main()
{
int x[2][2] = {0};
size_t i = 0;
size_t j = 0;
func(x[0]);
for(i = 0; i < sizeof x / sizeof *x; ++i)
for(j = 0; j < sizeof *x / sizeof **x; ++j)
std::cout << "x[" << i << "][" << j << "] == "
<< x[i][j] << '\n';
return 0;
}
-Mike
}
-Mike
"Dmitrii PapaGeorgio" <vr****@woh.rr.comNOSPAM> wrote in message
news:bM*********************@twister.neo.rr.com... i'm using array and not a container since my teacher is a fruit cake.
It's rarely a good idea to use epithets to describe
someone who probably has much more knowledge than you do.
Are you sure your teacher doesn't read posts here?
If (s)he does, and sees this post from you, how do
you think you'll be treated in his/her class in the
future? Just a thought ....
Your post didn't mention anything about a teacher or
that you're taking a class. Your teacher is probably
trying to teach you about arrays. That doesn't make
him/her a 'fruitcake', but someone who is teaching
part of the language. Arrays do have their place
in C++ programs, but often a container is a better
choice for some tasks. That issue does not apply
here, since your array is just part of a learning
exercise and not a 'real' application.
BTW please don't top post.
-Mike
thanks guys - better lectures in here than in class...hope she doesn't
read this thread (teacher)
Kevin Goodsell wrote: Jonathan Mcdougall wrote:
The same as an int :
int x = 5; f(x);
// takes an int void f(int i) { i = 2; }
The reason for which 'i' is of type 'int' is because 'x' is of type int.
Now if 'x' is of type 'int[2][2]', well 'i' will be too :
Actually, in nearly all contexts 'x' will be of type int (*)[2] (pointer to array of two ints). Array names usually become pointers to the type of the objects stored in the array. There are exceptions to this, for example when the name is used as the operand of the sizeof or unary & operators.
void f(int i[2][2])
Equivalent to
void f(int (*i)[2])
{ i[0][0] = 4; }
But if you allocated the array with new, you probably did something like
int size_1 = 2; int size_2 = 2;
int **x = new int*[size_1]
for ( int i = 0; i<size_1; ++i) x[i] = new int[size_2];
Since 'x' is now of type 'int**', that is what 'i' will be :
void f(int **i) { i[0][0] = 4; }
The 'problem' with that is now you have no idea about the bounds of 'i'.
This isn't a new problem. You had the same problem before:
void f(int i[2][2]) { //... }
This doesn't give the size of the array i (which isn't actually an array), it just looks like it does. That first '2' is absolutely useless - the compiler ignores it.
-Kevin
Dmitrii PapaGeorgio wrote: thanks guys - better lectures in here than in class...hope she doesn't read this thread (teacher)
This is the third time this has been mentioned in this thread: Please
don't top-post. Read section 5 of the FAQ for posting guidelines. http://www.parashift.com/c++-faq-lite/
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
> > Now if 'x' is of type 'int[2][2]', well 'i' will be too : Actually, in nearly all contexts 'x' will be of type int (*)[2] (pointer to array of two ints). Array names usually become pointers to the type of the objects stored in the array. There are exceptions to this, for example when the name is used as the operand of the sizeof or unary & operators.
Thanks for clarification. void f(int i[2][2])
Equivalent to
void f(int (*i)[2])
Yes, but not (imho) better/clearer. void f(int **i) { i[0][0] = 4; }
The 'problem' with that is now you have no idea about the bounds of 'i'.
This isn't a new problem. You had the same problem before:
Yes, but since you wrote the dimension manually before compiling, you
know its size and can use magic numbers/constants. That is what I meant. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: overbored |
last post by:
I can do this:
int asdf;
int* zxcv = asdf;
but not this:
int asdf;
int** zxcv = asdf;
|
by: Peter B. Steiger |
last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C
is a simple word game that involves stringing together random lists of
words. In the Pascal version the whole array was...
|
by: sangeetha |
last post by:
Hello,
Is there any performance difference in using of the following two declaration?
int (*ptr); //Array of 10 int pointers
int *ptr; // pointer-to-array of 10.
Regards,
Sangeetha.
|
by: x-pander |
last post by:
given the code:
<file: c.c>
typedef int quad_t;
void w0(int *r, const quad_t *p)
{
*r = (*p);
}
|
by: anonymous |
last post by:
I have couple of questions related to array addresses. As they belong
to the same block, I
am putting them here in one single post. I hope nobody minds:
char array;
int address;
Questions...
|
by: Pedro Graca |
last post by:
I run into a strange warning (for me) today (I was trying to improve
the score of the UVA #10018 Programming Challenge).
$ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
|
by: Michael |
last post by:
Hi,
I am trying to pass a function an array of strings, but I am having trouble
getting the indexing to index the strings rather than the individual
characters of one of the strings.
I have...
|
by: mdh |
last post by:
>From p112 ( K&R).
Given an array declared as
static char arr= { { 0,1,........},{0,1,.....}};
let arr be passed as an argument to f.
f( int (*arr) ) {....}
It is noted that the...
|
by: Imran |
last post by:
Hello all, I am trying to pass 2D array to a func, as follows, it is showing
compiler error.
void helloptr(int **a){
a = 0xB;
}
int main(int argc, char* argv){
|
by: subramanian100in |
last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list ·
Question 6.13
int a1 = {0, 1, 2};
int a2 = {{3, 4, 5}, {6, 7, 8}};
int *ip; /* pointer to int */
int (*ap); /* pointer to...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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...
| |