Address/pointer and string

Source: Internet
Author: User

When doing your homework today, I found one of the following problems.

The home page is this self-compiled 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);p UTS (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]= '; for (i=0;i <n;i++) s1[i]=s2[i];}}

Forget it all: The address of the array is passed in the argument.

So the address of the S1,S2 in the main function is passed into the s1,s2 of the strata function.

Then has been entangled in the relationship between string and address, slowly found Char *prt=s1;

A string can assign a value to an address problem:

For statement char *a= "Hello";

For this declaration, the misconception is that a character pointer is declared (it points to a location) and the string is assigned to the address pointed to by the pointer expression "*a". But the positive solution is: After a character pointer is declared, it is assigned 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 a character pointer; 2. Allocate memory to a string; 3. Assign the first address of the string to a character pointer; Here are two points to consider:*a just points to a character. Examples are as follows:

[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 characters, using "%c" * /
  6. printf ("Output string:%s/n", a); / * Output string, use "%s", and a cannot have an asterisk "*" * /
  7. System ("pause"); / * In order to see the output results * /
  8. }
  9. /* Run the results as follows:
  10. Output character:b<br> output character: C
  11. Output string: bcd*/

If a string constant appears in an expression, the value represented is the address of the first character of the string constant. So "Hello" just stands for its address.            

    1. Char *a;
    2. a="Hello"; /* Here the string "Hello" represents only the address of its first character * /

There are no string variables in C, so character arrays are generally used when storing string constantsto store.
The array is a contiguous storage space, where two important quantities are recorded: the first address and the space size.
    and the pointer does not care about its length, as long as the record ere address is OK.

    Char *a;//request space Address
    a= "Hello";//a points to ' H ', and memory is open.
     for ease of understanding, some places can be "equivalent" to the address/pointer and the string, the address/pointer to the variable is the first character of the string.

     If you have a better understanding, I hope you will review







Address/pointer and string

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.