CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. BVW
Junior Member
Join Date
Dec 2008
Posts
3

## C++ pointers confusion

Quick question for one of you experts out there.

I am not a C++ programmer, but I am trying to convert a block of code into another language.
The below is a troublesome loop that I do not undestand.
what the heck is this for loop doing? I do not understand srcmtx[i][k]+4.
What does the +4 mean?

I thought that the +4 would be increasing the element. e.g. arr[1][2]+4 is the same as arr[1][9]

but seeing the +20 blows that theory out of the water..

Can anyone explain this to me?

Code:
```static char *srcmtx[1][11] = {
"                        ",
"            AAA         ",
" BB          CD         ",
"                        ",
"AAA          DD         ",
"         X   Y       Z  ",
"        NNN             ",
"                        ",
"                        ",
" BB          X          ",
"                        ",
};

for(k = 0;k < 11; k++) {
if (memcmp("   ",(srcmtx[i][k]+4),3))
memcpy(targetmtx[k*6+1],(srcmtx[i][k]+4),3);
if (memcmp("   ",(srcmtx[i][k]+8),3))
memcpy(targetmtx[k*6+2],(srcmtx[i][k]+8),3);
.
.
.
if (memcmp("   ",(srcmtx[i][k]+20),3))
memcpy(targetmtx[k*6+5],(srcmtx[i][k]+20),3);
}};```

thx,
BVW

2. ## Re: C++ pointers confusion

Where on earth did you find this code? It is quite odd.
I dont think there is a point to writing array[1][11]. It is the same as array[11] and redundant.
To write *parray[1][11] is really strange. What +20 is trying to do is add 20 bytes to the source
address. Honestly it doesnt look like the original code that you are posting...could you be trying
to recreate this code from memory?
Last edited by ahoodin; December 15th, 2008 at 10:32 PM. Reason: odd

3. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: C++ pointers confusion

The only possible valid value for i there is 0, so I'm assuming that's what it is; but I have no idea that targetmtx is, or what that code might be attempting to do. That's just a *really* strange array of strings......

However, assuming i is 0, then k is indicating which of those strings, and the "+n" is indicating how many characters into the string. In general:
Code:
```type array[20], *temp;
temp = array + 5;
// is equivalent to
temp = &array[5];```

4. BVW
Junior Member
Join Date
Dec 2008
Posts
3

## Re: C++ pointers confusion

Sorry, the original size of the variable is actually [16][11]. But I didnt think I needed to post the whole massive declaration to get the point across.

The value for i is actually passed into the function as a parameter, meaning it is not part of a loop.

Glad to hear I am not the only one puzzled by this then.

The only thing I cant figure out is what that +4, or +20 means. Each element looks to be only 11 characters long, so adding 20 to it seems like nonsense..

Thanks,
Brian

5. ## Re: C++ pointers confusion

Originally Posted by BVW
The only thing I cant figure out is what that +4, or +20 means. Each element looks to be only 11 characters long, so adding 20 to it seems like nonsense..
There are 11 pointers to 'strings', but each 'string' is 24 characters long, so +20 is accessing the 20th character where srcmtx[i][k] is the pointer to the start of the 'string'

What language are you trying to convert it to?

6. BVW
Junior Member
Join Date
Dec 2008
Posts
3

## Re: C++ pointers confusion

Ahh I see.

So arr[0][2]+1 would be 'BB '

What a strange syntax.

I am converting to .net.

Thanks for the help.

7. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: C++ pointers confusion

Originally Posted by BVW
Ahh I see.

So arr[0][2]+1 would be 'BB '

What a strange syntax.
Well, not exactly. See, you aren't accessing an element of the string, you're advancing a pointer from the start of it. arr[0][2][1] would be the character 'B'; arr[0][2]+1 would be a pointer to the C-style string starting with that letter. So what you've got is a pointer to the string
Code:
`"BB          CD         "`
, but since the memcmps only check three characters, you are indeed comparing to "BB ".
Last edited by Lindley; December 16th, 2008 at 10:09 AM.

8. ## Re: C++ pointers confusion

Originally Posted by BVW
What a strange syntax.
Indeed, even for a 'C' program

9. ## Re: C++ pointers confusion

Originally Posted by BVW
Ahh I see.

So arr[0][2]+1 would be 'BB '

What a strange syntax.

I am converting to .net.

Thanks for the help.
No this is ugly code even for C. I am afraid it is not even a very good representation of C code. There are far better sources to learn to program from:

http://www.cprogramming.com/
http://www.cplusplus.com/

CG has great forums and FAQs for specific questions as well. CG has great COM tutorials and such.

10. ## Re: C++ pointers confusion

Hopefully here's the same functionality in C++.
It may be closer to the syntax of the language you are converting to than the original code.

Code:
```#include <vector>
#include <string>
#include <array>

// Array of 11 strings.
std::tr1::array<const std::string, 11> srcmtx =
{
"                        ",
"            AAA         ",
" BB          CD         ",
"                        ",
"AAA          DD         ",
"         X   Y       Z  ",
"        NNN             ",
"                        ",
"                        ",
" BB          X          ",
"                        ",
};

// 100 target strings.
std::vector<std::string> targetmtx(100);

int main()
{
for (size_t k = 0; k < srcmtx.size(); ++k)
{
if (srcmtx[k].compare(4, 3, "   "))
{
targetmtx[k * 6 + 1] = srcmtx[k].substr(4, 3);
}

if (srcmtx[k].compare(8, 3, "   "))
{
targetmtx[k * 6 + 2] = srcmtx[k].substr(8, 3);
}

if (srcmtx[k].compare(20, 3, "   "))
{
targetmtx[k * 6 + 5] = srcmtx[k].substr(20, 3);
}
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•