4144 views|14 replies

411

Posts

9

Resources
The OP
 

Why can the defined pointer be used as an array? [Copy link]

 

/*Clear screen function--clear the entire screen to the same color*/
void lcd_clear(unsigned int color)
{
unsigned int num;
unsigned int i = 0;
unsigned int *startaddr = (unsigned int*)tftlcd_dev.framebuffer;
num = (unsigned int)tftlcd_dev.width * tftlcd_dev.heigh;
for(i = 0; i < num; i++)
{
startaddr[i] = color;
}

}

This is the program I encountered when learning the Linux development board.

What I don't understand is that the program defines unsigned int *startaddr as a pointer, so why is it used directly as an array when it is used later? startaddr[ i ] is clearly the way to use an array, so how can a pointer and an array be used together? What is this syntax?

This post is from Linux and Android

Latest reply

Because the data stored in the array is continuous in memory, the pointer index group refers to the address of the first element of the array, so adding 1+1 to the pointer can read or write to the array.   Details Published on 2024-3-7 09:40

411

Posts

9

Resources
2
 

I think I found the answer myself. This link says https://blog.csdn.net/qq_25814297/article/details/87445481?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-87445481-blog-102632978.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-87445481-blog-102632978.pc_relevant_default&utm_relevant_index=2

This post is from Linux and Android

Comments

Data exchange between arrays or memory often uses pointers.  Details Published on 2022-7-18 09:40
 
 

143

Posts

1

Resources
3
 

An array is a continuous storage unit in memory. The pointer points to the head of this memory data block to access data, which is no different from array operations. Be careful when using pointers to avoid crossing the boundary.

This post is from Linux and Android
 
 
 

7462

Posts

2

Resources
4
 

Equivalent in a certain sense

This post is from Linux and Android
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

114

Posts

6

Resources
5
 

I have discovered this, it is quite special, and I often use it interchangeably.

This post is from Linux and Android
 
 
 

2

Posts

1

Resources
6
 

The array name is the address

This post is from Linux and Android
 
 
 

6069

Posts

4

Resources
7
 
This post was last edited by damiaa on 2022-7-18 09:43
shijizai published on 2022-5-19 10:41 I seem to have found the answer myself. This link says https://blog.csdn.net/qq_25814297/article/details ...

Data exchange between arrays or memory often uses pointers. C language pointers are very flexible. They can hit whatever you point to. If you point to the wrong place, it will still hit. But that will cause trouble.

This post is from Linux and Android
 
 
 

2

Posts

0

Resources
8
 

Array names and pointers are essentially memory addresses and are equivalent.

This post is from Linux and Android
 
 
 

224

Posts

0

Resources
9
 

Take a look at the disassembled code and you will know what it is doing behind the scenes.

This post is from Linux and Android
 
 
 

205

Posts

0

Resources
10
 

The array name is the first address of the data in the memory. The array data is a continuous storage unit in the memory. The pointer can point to the first address of the array.

This post is from Linux and Android
 
 
 

894

Posts

0

Resources
11
 

The internal array name of the C compiler is treated as a pointer, that is, the first address.

This post is from Linux and Android
 
 
 

205

Posts

0

Resources
12
 

Array names and pointers are related but also different. Array names are constant types, while pointers are variable types. You cannot assign a pointer to an array name.

This post is from Linux and Android
 
 
 

364

Posts

0

Resources
13
 

Print the pointer address and you will know. There is also a simple way to verify it, * (ptr + x * sizeof (type)) to see if it is equal to ptr [x]

This post is from Linux and Android
 
 
 

205

Posts

0

Resources
14
 

This knowledge is worth further study and I hope to provide information on this topic for further study.

This post is from Linux and Android
 
 
 

1

Posts

0

Resources
15
 

Because the data stored in the array is continuous in memory, the pointer index group refers to the address of the first element of the array, so adding 1+1 to the pointer can read or write to the array.

This post is from Linux and Android
 
 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list