473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

pointer, array arithmetic

1
question about pointer and array.
can we add a 2-D array (**A) with a 1-D array (*B)? both of the array is define as a pointer in the program.
where array A is A[i][j] and B is B[i]

how to get call back a value of a pointer? for example A[i][1] and is it possible to redefine it as a 1-D array?

i find that the pointer is the most confusing thing in C language....need help...
Mar 23 '06 #1
1 3400
Banfa
9,065 Expert Mod 8TB
An array is just a section of memory large enough to contain a number of values be it 1, 2 or more dimensions. However memory is physical and (generally) is only 1 dimensional.

If your arrays are defined as so

#define DIMENSION_1 10
#define DIMENSION_2 20

typedef <TypeInQuestion> TYPE;

TYPE A[DIMENSION_2][DIMENSION_1];

TYPE B[DIMENSION_1];

then A[n] has the same type as B and could be copied to B (or vis versa).

Arrays are not a thing and can be "added" and I am not quite sure what you mean by this.

However defining C like this

TYPE **C;

and the array A defined above are not the same thing at all. The array A has set dimensions and has memory allocated to hold all values you can access any element by A[i][j].

C is just a pointer, it is not an array at all but a pointer to a pointer to TYPE. C[i][j] does not do what you thing it does, and certainly not the same as A[i][j].

C is pointer to pointer to type, so C is a pointer if you use it as an array, C[i], you will reference an array that has type pointer to TYPE (TYPE *) not an array of type TYPE [DIMENSION_1] as A[i] would be.

since sizeof(TYPE *) is unlikely to be equal to sizeof(TYPE [DIMENSION_1])

A[i] and C[i] are not equivilent.

If you want to use a pointer as a 2 dimensional array then you do it like this

TYPE D[DIMENSION_2][DIMENSION_1];
TYPE *E = &D[0][0];

then

D[i][j]

and

E[i*DIMENSION_1+j]

will access the same element of D.
Mar 23 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Alex Fraser | last post by:
From searching Google Groups, I understand that void pointer arithmetic is a constraint violation, which is understandable. However, generic functions like qsort() and bsearch() must in essence do...
10
by: houstorx | last post by:
I've been looking at the committee draft of the C99 specification, specifically the one at this URI: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n843.pdf. I don't have a copy of the official...
6
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
5
by: jdcrief | last post by:
I have all of this program working except for the pointer arithmetic part, I think. This program should average the student's grades together using pointer arithmetic and then display the...
5
by: jerry | last post by:
I need to modify the code of a command-line tool. The source code starts out like this: int main(int argc, char *argv) { int ch, A, B ; while ((ch = getopt(argc, argv, "AB")) != -1)...
24
by: Kavya | last post by:
int main (){ int a={{1,2,3},{4,5,6}}; int (*ptr)=a; /* This should be fine and give 3 as output*/ printf("%d\n",(*ptr)); ++ptr;
4
by: Bernd Gaertner | last post by:
Dear experts, according to my interpretation of the Standard, loop 1 in the following program is legal, while loop 2 is not (see explanation below). This looks a bit counterintuitive, though; do...
25
by: Ioannis Vranos | last post by:
Are the following codes guaranteed to work always? 1. #include <iostream> inline void some_func(int *p, const std::size_t SIZE) {
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.