About char str, pointer, and *p.

Asked 1 years ago, Updated 1 years ago, 366 views

For the following code, can the char type str[] only be displayed before the null is in the middle of the string?

Also, can the pointer p hold only one memory address that the address points to?

For example, do you have a string and only have the first address of the string from the beginning of the string to the front of the null?

Also, does *p only handle the first value of the address?If so, can I only use %c to represent that number?

#include<stdio.h>
# include <string.h>

int main(void){

    char str[] = "str==NULL?\"(NULL)\":str";
    char*p,*q;
    intch;
    p=str;

    for(;;){

        for(q=p;!(*q=='?'||*q==':'||*q==0);q++);

        ch=*q;
        *q = 0;

        printf("|%s|\n",p);

        if(ch==0)break;

        p = q+1;
    }
}

c pointer

2022-09-30 21:56

2 Answers

was developed in the 1970s, so it only stipulates specifications that can be handled easily on computers at that time.More features are built into 21st century programming languages, but has such a promise (= specification), so A1. is Yes, but if you don't want to use the standard function, you can implement it as you like.It doesn't say exactly what you really want to do, so you can't say exactly what you want to do.

The pointer to **** is a specification that represents the entire **** with its first address, so A2. is also Yes

A3.

#Leave some strictness behind

For example, if charc;char*p=&c; has a p value of 0x4321, the 0x4321 value points to all eight bits.

Similarly, if the value of q is 0x5678 and the value of q is 0x5678 is 64 bit, the value of 0x5678 points to the bit(by code) of 0x5678 to 0x567F is all

If there is a pointer structurevery_large_structure*r=&vls; pointing to structurevery_large_structure{...};, similarly r has a value of 0x3210, the whole structure can refer to 0x3210.

*p refers to 8 bits (one char), so even if it is a character, it is not a column; *q refers to one double and *r refers to one very_large_struct.


2022-09-30 21:56

A string in C language is defined as an array of characters with a termination of '\0' (zero), so

If there is a null, can it only be displayed before there is a null?

It's not that, but that the null (zero) part is the termination.

Also, can the pointer p only hold one memory address that the address points to?

That's right.
However, if it is an array, the element is placed at a continuous address, so it can be treated as the address of the array

Also, does *p only deal with the first value of the address?If so, can I use only %c to represent that number?

As I said earlier, a string is an array of characters.
* If you say p, it will be the address, so even if p points to a string, characters will appear.
If you want to treat it as a string, pass p as %s


2022-09-30 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.