473,385 Members | 1,570 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,385 software developers and data experts.

pointer arithmetic

I'm having some trouble interpreting some legacy code...here's a single
line of the kind of pointer arithmetic that baffles me. I need help
both interpreting and understanding the reasoning behind it. Can
someone help me?

*(ioStruct.FrntScrPtrn + uj *(*ioStruct.FrntScrPtrn_NumItemsY)+ui) =
tempChar;

can someone re-write this for me in terms I can understand? i.e., my
first attempt was this, but it's incorrect:

ioStruct.FrntScrPtrn[ uj * ioStruct.FrntScrPtrn_NumItemsY[ui] ] =
tempChar;

Also, is there a purpose for this form of pointer arithmetic? Is it
faster?

Many thanks,
sharon

Sep 1 '06 #1
5 1285
dotnetchic wrote:
I'm having some trouble interpreting some legacy code...here's a
single line of the kind of pointer arithmetic that baffles me. I
need help both interpreting and understanding the reasoning behind
it. Can someone help me?

*(ioStruct.FrntScrPtrn + uj *(*ioStruct.FrntScrPtrn_NumItemsY)+ui) =
tempChar;

can someone re-write this for me in terms I can understand? i.e., my
first attempt was this, but it's incorrect:

ioStruct.FrntScrPtrn[ uj * ioStruct.FrntScrPtrn_NumItemsY[ui] ] =
tempChar;

Also, is there a purpose for this form of pointer arithmetic? Is it
faster?
This calculated an index into a 2-dimensional array of variable size:

uj is the "x coordinate", ui is the "y coordinate",
ioStruct.FrntScrPtrn_NumItemsY is the width of the array in the "Y"
dimension, and the array is stored in "column major order" (all points with
the same Y coordinate value are contiguous in the array).

The purpose is simple: C and C++ don't have any built-in concept of a
variable-sized array, so there's no way the compiler can know the length of
the rows in the array at compile time (and it's necessary to know the length
of the major axis of a 2-D array to calculate the offset to an element in
the array).

HTH

-cd
Sep 1 '06 #2
"dotnetchic" <do********@gmail.comwrote in message news:11**********************@m73g2000cwd.googlegr oups.com...
I'm having some trouble interpreting some legacy code...here's a single
line of the kind of pointer arithmetic that baffles me. I need help
both interpreting and understanding the reasoning behind it. Can
someone help me?

*(ioStruct.FrntScrPtrn + uj *(*ioStruct.FrntScrPtrn_NumItemsY)+ui) =
tempChar;

can someone re-write this for me in terms I can understand? i.e., my
first attempt was this, but it's incorrect:

ioStruct.FrntScrPtrn[ uj * ioStruct.FrntScrPtrn_NumItemsY[ui] ] =
tempChar;
ioStruct.FrntScrPtrn[ui+ uj * (*ioStruct.FrntScrPtrn_NumItemsY)] =tempChar;

Also, is there a purpose for this form of pointer arithmetic? Is it
faster?
No, not faster. A decent compiler would create same code.
But a pointer and array are actually different concepts; so it really
depends whether the author sees it as array, or not.

--PA
Sep 1 '06 #3
On 1 Sep 2006 08:22:59 -0700, "dotnetchic" <do********@gmail.comwrote:
>I'm having some trouble interpreting some legacy code...here's a single
line of the kind of pointer arithmetic that baffles me. I need help
both interpreting and understanding the reasoning behind it. Can
someone help me?

*(ioStruct.FrntScrPtrn + uj *(*ioStruct.FrntScrPtrn_NumItemsY)+ui) =
tempChar;

can someone re-write this for me in terms I can understand? i.e., my
first attempt was this, but it's incorrect:

ioStruct.FrntScrPtrn[ uj * ioStruct.FrntScrPtrn_NumItemsY[ui] ] =
tempChar;

Also, is there a purpose for this form of pointer arithmetic? Is it
faster?
It looks like someone is simulating 2D array access with a pointer into a
1D array. If I knew exactly what ui, uj, and NumItemsY represent, I could
explain it specifically, but the general pattern for turning a[i][j] into a
pointer expression is this:

T* a1d = (T*) a;
assert(a1d+i*NumCols+j == &a[i][j]);

If ui is the row number, uj the column number, and NumItemsY the number of
rows, then your expression represents column-major access (as in Fortran)
vs. the normal row-major access of C++.

--
Doug Harrison
Visual C++ MVP
Sep 1 '06 #4
Thanks to you all for the quick responses. I have posted the more
complete snippet this time.

for (uj = 0; uj < * ioStruct.FrntScrPtrn_NumItemsX; uj++)
for (ui = 0; ui < * ioStruct.FrntScrPtrn_NumItemsY; ui++)
{
fread(&tempChar, sizeof(tempChar), 1, fInput);
*(ioStruct.FrntScrPtrn + uj
*(*ioStruct.FrntScrPtrn_NumItemsY)+ui) = tempChar;
}

Carl Daniel [VC++ MVP] wrote:
The purpose is simple: C and C++ don't have any built-in concept of a
variable-sized array, so there's no way the compiler can know the length of
the rows in the array at compile time (and it's necessary to know the length
of the major axis of a 2-D array to calculate the offset to an element in
the array).
I don't *believe* these are variable sized arrays, but I could be
wrong. If so, how would overruns be handled (I'm guessing silently
ignored and trashed data/data pointers)?

Sep 1 '06 #5
"dotnetchic" <do********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Thanks to you all for the quick responses. I have posted the more
complete snippet this time.

for (uj = 0; uj < * ioStruct.FrntScrPtrn_NumItemsX; uj++)
for (ui = 0; ui < * ioStruct.FrntScrPtrn_NumItemsY; ui++)
{
fread(&tempChar, sizeof(tempChar), 1, fInput);
*(ioStruct.FrntScrPtrn + uj
*(*ioStruct.FrntScrPtrn_NumItemsY)+ui) = tempChar;
}
That's about the lamest loop I've ever seen. Obviously written for
employment security. It's equivalent to the slightly faster (calling T the
element type of ioStruct.FrntScrPtrn):

const size_t colcount = *ioStruct.FrntScrPtrn_NumItemsY;
size_t rowsleft = *ioStruct.FrntScrPtrn_NumItemsX;
T* p = ioStruct.FrntScrPtrn;
while (rowsleft-- 0)
{
size_t colsleft = colcount;
while (colsleft-- 0)
{
fread(&tempChar, sizeof(tempChar), 1, fInput);
*(p++) = tempChar;
}
}

But that still calls fread a bunch of times, so we'll speed that up with:

when sizeof(T) == sizeof(tempChar),

const size_t colcount = *ioStruct.FrntScrPtrn_NumItemsY;
const size_t rowcount = *ioStruct.FrntScrPtrn_NumItemsX;
fread(ioStruct.FrntScrPtrn, sizeof(*ioStruct.FrntScrPtrn), colcount *
rowcount, fInput);

or, when sizeof(T) != sizeof(tempChar), calling T1 the type of tempChar

const size_t colcount = *ioStruct.FrntScrPtrn_NumItemsY;
const size_t rowcount = *ioStruct.FrntScrPtrn_NumItemsX;
size_t elems = colcount * rowcount;
T1 *tempBuffer = (T1*)alloca(elems * sizeof (T1)); // pick your favorite
allocator
fread(tempBuffer, sizeofT1), elems, fInput);
while (elems-- 0)
ioStruct.FrntScrPtrn[elems] = tempBuffer[elems]; // compiler should
convert this to a pair of pointers...
// deallocate tempBuffer here if not using alloca or garbage collection
Sep 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ceo | last post by:
Hi, Following is a program that doesn't give the expected output, not sure what's wrong here. I'm adding the size of derived class to the base class pointer to access the next element in the...
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...
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; }
3
by: randomtalk | last post by:
hello everyone! Well, recently i've been trying to pick up c and see what is pointer all about (been programming in lisp/python for the better part of my last two years).. mmm.. I'm currently...
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...
7
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
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...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
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,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
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...

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.