Address/pointer and string, address/pointer string

Source: Internet
Author: User

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?
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. Int main (void ){
  4. Char * a = "bcd ";
  5. Printf ("output character: % c/n", * a);/* output character, using "% c" */<br> printf ("output character: % c/n ", * (a + 1);/* output character, using" % c "*/
  6. Printf ("output string: % s/n", a);/* output string, using "% s"; and a cannot have a star number before "*"*/
  7. System ("pause");/* in order to see the output result */
  8. }
  9. /* The running result is as follows:
  10. Output character: B <br> output character: c
  11. 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.

 
  1. Char *;
  2. 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.







 

 

Related Article

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.