Char * pointer and char pointer

Source: Internet
Author: User

Char * pointer and char pointer

Some time ago I wrote a difference between char * and char []. Today I have a deeper understanding of char *.

1: Like other pointers, the char pointer also defines an address. For example, int * defines a stack, 4 bytes, and char *. Now, write a piece of code.

#include<iostream>using namespace std;int main(){int a=12;char b='67';int *c=&a;char *d=&b;return 0;}

Memory Distribution:


In this case, char * Indicates a simple character. In this case, int occupies 4 bytes and char occupies 1 byte.

2: If char * is used to represent a string, I will roughly understand in the previous article that char * represents string storage. It is written into the stack as a pointer address, the stack stores the address pointing to the rodata region segment, the char * string content is stored in the rodata segment, and the c ++ memory storage allocation defines that the rodata region is a read-only region, it is stipulated that the data stored in the read-only area cannot be changed and can only be read. In this way, write a new piece of code to verify

#include<iostream>using namespace std;int main(){char *p="hello";return 0;}

Here, I need to know the address of the p pointer, the value of p, and whether the value of p points to a new address.

First, we checked the number of bytes in the p pointer stack.


I checked the address of & p: its address is 0x0032faac, and its region storage value is 48 dd 93 00.

According to the explanation, the value of p's address is stored by stack, and its value should be the address directed to "hello, but what does the value of 48 dd 93 00 mean? Is it the address 0x0093dd48 according to the stack's inbound order? Is it the address that points to the rodata region of p, is this address "hello.

I tried to hit the address stored in the stack 0x0093dd48.


We found that this memory address stores the "hello" ascii code. Because it is the memory in the read-only area, we try to think about its transmission mechanism and change the stack or point to the address, or you can change the content of the specified address to see if the error meets your pre-requirements. I don't believe it here.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.