hello,

Here's a simple question I want to clear up.
We all know that we can use the name of an array as the pointer to that array, and that a pointer holds the address to the variable named.

What I'm doing is this

//somwhere in the code
uint8_t myspace[100];

//elsewhere in the code
uint8_t *pmyspace = myspace;

//and again somewhere different
uint8_t array[256];

//Is this correct if I want to copy the 100 values in myspace to array?

array = pmyspace;

//Or Should I go one by one
for (int i=0; i<100; i++)
{
    array[i] = *(pmyspace+i);
}

That's it,

Thanks in advance,

T

Recommended Answers

All 4 Replies

> //Is this correct if I want to copy the 100 values in myspace to array?
Had you tried it, you would have gotten a compilation error.

The answer to the other question is yes.

> *(pmyspace+i);
You can even write pmyspace[i];

I know this is knit picking...

We all know that we can use the name of an array as the pointer to that array,

The name of an array is a label which is a convenient human readable memory address. Its not a pointer...

Thanks a lot for your answers, and it's true I could've tried it, but I like talking so that simple questions leave answers in here. I wish C had a shell like python, then I wouldn't go directly to a forum rather than starting a whole new project for sucha simple quesiton in the environment I use.

And I know the NAME of the array is not a POINTER to it, I said we can use it as a pointer... Knit picking is good, people learn a lot from it so keep at it...

Thanks again,

T

I know this is knit picking...

We all know that we can use the name of an array as the pointer to that array,

The name of an array is a label which is a convenient human readable memory address. Its not a pointer...

Actually, the statement you quoted is more correct than yours. An array name is converted to a pointer to the first element when used in value context. There are only three cases where an array name is not used in value context, so it's true that an array name is a pointer, after a fashion.

The statement you quoted was missing the "pointer to the first element" part, which is the only thing I would nitpick about it, given no other details.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.