Home > Back-end >  There is a problem about C language array and pointer
There is a problem about C language array and pointer

Time:11-29

Is this picture program, is selected the two sentences, why use array can output, can't use pointer output

CodePudding user response:

Use Pointers you must first initialize the pointer, it points to a valid memory
For example,
Char * a=(char *) malloc (sizeof (char) * 32);//a memory to apply for the
Copy (a, STR);
Free (a);//if it is your application memory, memory use after their release

CodePudding user response:

Can't be as a char * I

CodePudding user response:

Correct answer: as shown in figure was made of array can output, but also can output using a pointer, but the topic main code has a problem,
Problem: an array of char a [10], the default is defined as a 10 elements of one dimensional array, while the use of pointer must point to a memory address, therefore there needs to be defined, you can assign a new address, can also point to existing quantity, because the variables are the values in the memory space, and the name of the memory space is address, pointer points to address, hope to adopt ~

CodePudding user response:

Can use malloc to char * a distribution of a block of memory, so that you can use

CodePudding user response:

Got it, thank you, I didn't give Pointers is that the original distribution of space

CodePudding user response:

Very happy to help you, hope knot stick ~ as soon as possible

CodePudding user response:

Dictionary is not a directory, the dictionary to find specific pronunciation of a word, meaning, first have to find the directory which [the line], according to the back pages, turn over to see the right,
The line like a pointer, you set up an entry, but the back of the pages corresponding pointer to address [] is unknown, could be anywhere, intent to access the data by the random address, such behavior nature is not permitted,

CodePudding user response:

Char * a; Is wild pointer that points to the position are random, pointing to a region of memory is not necessarily can be written, so if a point not to write, then copy function is written, can appear abnormal, less error,

With an array, then the definition of space is on the stack, on the stack space can be read and write,

CodePudding user response:

You * a is a pointer to the wild, it does not point to a specific piece of memory, you to copy it is pointing to him the memory operations, but it points to the unknown, it is possible that a piece of memory, are not allowed to access will be submitted to the section of error, you can use malloc: char * a=malloc (16); Then ok,
If you define a [] an array system will automatically be assigned to it in the stack a piece of memory, can be accessed,

CodePudding user response:

Char * STR="abcdefg";
"Abcdefg" is a literal constant, a block is stored in the constant area, here is the "abcdefg" in the constant area address to STR pointer
Char a, [10].
Declare a capacity of 10 array, a is a local variable, in the stack area for a distribution of a 10 bytes of memory
Copy by STR store address into constant area of memory copy "abcdefg" into an array of memory
Char * a;
Declare a pointer to a pointer refers to memory with char read format, pointer a and STR has its own memory, is used for backup address, can not be used to store the string,
A no initialization, so a itself is a garbage value of memory storage, which is not clear to the pointer is wild pointer, to read and write in wild pointer "target memory triggers section error!" A program crash,

CodePudding user response:

Feel the help, again not knot stick, a few you mean

CodePudding user response:

skzeng @ 11 references, ros response:
think help, again not knot stick, a few you mean

This I don't know where is the mobile version of the junction post

CodePudding user response:

reference response: 7th floor thousand dream life
dictionary is not a directory, the dictionary to find specific pronunciation of a word, meaning, first have to find the directory which [the line], according to the back of the page, turn over to see again right,
The line like a pointer, you set up an entry, but the back of the pages corresponding pointer to address [] is unknown, could be anywhere, intent to access the data by the random address, such behavior nature is not permitted,

Thanks for elder brother, I see

CodePudding user response:

references to the tenth floor spark response: 20180731
char * STR="abcdefg";
"Abcdefg" is a literal constant, a block is stored in the constant area, here is the "abcdefg" in the constant area address to STR pointer
Char a, [10].
Declare a capacity of 10 array, a is a local variable, in the stack area for a distribution of a 10 bytes of memory
Copy by STR store address into constant area of memory copy "abcdefg" into an array of memory
Char * a;
Declare a pointer to a pointer refers to memory with char read format, pointer a and STR has its own memory, is used for backup address, can not be used to store the string,
A no initialization, so a itself is a garbage value of memory storage, which is not clear to the pointer is wild pointer, to read and write in wild pointer "target memory triggers section error!" Cause the program crashes,

Thank you very much! Hard, I see
  • Related