473,811 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Frnt ScrPtrn + uj *(*ioStruct.Frn tScrPtrn_NumIte msY)+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.FrntSc rPtrn[ uj * ioStruct.FrntSc rPtrn_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 1332
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.Frnt ScrPtrn + uj *(*ioStruct.Frn tScrPtrn_NumIte msY)+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.FrntSc rPtrn[ uj * ioStruct.FrntSc rPtrn_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.FrntSc rPtrn_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********@gma il.comwrote in message news:11******** **************@ m73g2000cwd.goo glegroups.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.Frnt ScrPtrn + uj *(*ioStruct.Frn tScrPtrn_NumIte msY)+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.FrntSc rPtrn[ uj * ioStruct.FrntSc rPtrn_NumItemsY[ui] ] =
tempChar;
ioStruct.FrntSc rPtrn[ui+ uj * (*ioStruct.Frnt ScrPtrn_NumItem sY)] =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********@gma il.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.Frn tScrPtrn + uj *(*ioStruct.Frn tScrPtrn_NumIte msY)+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.FrntS crPtrn[ uj * ioStruct.FrntSc rPtrn_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*Nu mCols+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.FrntSc rPtrn_NumItemsX ; uj++)
for (ui = 0; ui < * ioStruct.FrntSc rPtrn_NumItemsY ; ui++)
{
fread(&tempChar , sizeof(tempChar ), 1, fInput);
*(ioStruct.Frnt ScrPtrn + uj
*(*ioStruct.Frn tScrPtrn_NumIte msY)+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********@gma il.comwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.com.. .
Thanks to you all for the quick responses. I have posted the more
complete snippet this time.

for (uj = 0; uj < * ioStruct.FrntSc rPtrn_NumItemsX ; uj++)
for (ui = 0; ui < * ioStruct.FrntSc rPtrn_NumItemsY ; ui++)
{
fread(&tempChar , sizeof(tempChar ), 1, fInput);
*(ioStruct.Frnt ScrPtrn + uj
*(*ioStruct.Frn tScrPtrn_NumIte msY)+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.FrntSc rPtrn):

const size_t colcount = *ioStruct.FrntS crPtrn_NumItems Y;
size_t rowsleft = *ioStruct.FrntS crPtrn_NumItems X;
T* p = ioStruct.FrntSc rPtrn;
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.FrntS crPtrn_NumItems Y;
const size_t rowcount = *ioStruct.FrntS crPtrn_NumItems X;
fread(ioStruct. FrntScrPtrn, sizeof(*ioStruc t.FrntScrPtrn), colcount *
rowcount, fInput);

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

const size_t colcount = *ioStruct.FrntS crPtrn_NumItems Y;
const size_t rowcount = *ioStruct.FrntS crPtrn_NumItems X;
size_t elems = colcount * rowcount;
T1 *tempBuffer = (T1*)alloca(ele ms * sizeof (T1)); // pick your favorite
allocator
fread(tempBuffe r, sizeofT1), elems, fInput);
while (elems-- 0)
ioStruct.FrntSc rPtrn[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
2193
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 array. Why does bp point to some address that is not &d? Please clarify. Thanks,
22
12754
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 exactly this, and similarly generic functions seem to have useful applications. In a few posts I read, it was suggested to avoid void pointer arithmetic by casting to a char pointer, and performing arithmetic on that (in multiples of...
6
2297
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
2212
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 reading The C Programming Language Second Edition.. When i hit on pointer arithmetic example, i have no idea what's happening, hopefully some of you could alleviate my confusion, so here goes:
27
8986
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 advance. Erik
7
528
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
3067
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 read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
41
3693
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 pointer and reference are and how they relate to each other.
19
3908
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
2023
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) {
0
9726
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10384
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10395
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5553
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4338
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.