Address/pointer and string, address/pointer string
When I did my homework today, I found the following problems.
The homepage is the self-developed strncpy function:
#include "ctype.h"#include "stdlib.h"#include "string.h"#include "windows.h"int main(){char *strata(char s1[],char s2[],int n);char nam1[41]="das";char nam2[41]="wo shi yi ge da sha bi";strata(nam1,nam2,4);puts(nam1);system("pause");return 0;}char *strata(char s1[],char s2[],int n){int i;char *prt=s1;if(n>strlen(s2))return "-1";else{s2[n]='\0';for(i=0;i<n;i++)s1[i]=s2[i];}}
Previously, I suddenly forgot: the address of the array is input in the real parameter.
Therefore, the S1 and S2 addresses in the main function are passed into the s1 and s2 addresses of the strata function.
Then, the relationship between the string and the address is constantly entangled, and char * prt = s1 is gradually found;
The problem that a string can be assigned to an address:
For the statement char * a = "hello ";
This statement will causeMisunderstandingYes: declares a character pointer (which points to a position) and assigns the "string" to the address pointed to by the pointer expression "*. HoweverPositive SolutionYes: After declaring a character pointer, assign a value to the pointer variable a with the address of the first character of the String constant. That is, the correct order is: 1. allocate memory to character pointers; 2. allocate memory to strings; 3. Assign the first address of the string to the character pointer. Here there are two points to consider:①* A only points to one character. Example:
[C ++]View plaincopyprint?
- # Include <stdio. h>
- # Include <stdlib. h>
- Int main (void ){
- Char * a = "bcd ";
- Printf ("output character: % c/n", * a);/* output character, using "% c" */<br> printf ("output character: % c/n ", * (a + 1);/* output character, using" % c "*/
- Printf ("output string: % s/n", a);/* output string, using "% s"; and a cannot have a star number before "*"*/
- System ("pause");/* in order to see the output result */
- }
- /* The running result is as follows:
- Output character: B <br> output character: c
- Output string: bcd */
②If a String constant appears in an expression, it represents the address of the first character of the String constant. So "hello" only represents its address.
- Char *;
- A = "hello";/* the string "hello" only represents the address of the first character */
C does not contain string variables. Therefore, character arrays are generally used to store string constants.
An array is a continuous storage space, which records two important quantities: the first address and the size of the space.
The pointer does not care about its length, as long as it records the first address, it will be OK.
Char * a; // address of the requested space
A = "hello"; // a points to 'H' and the memory is opened.
For ease of understanding, the address/pointer can be equivalent to the string. The address/Pointer Points to the first character of the string.
If you have a better understanding, I hope you can comment on it.