[Dev C] C language string char question

Asked 2 years ago, Updated 2 years ago, 118 views

Write char*, which is used to input and output a string I heard that when you type and print out a string, you have to use the code charq[100]; You have to write a number after the variable name like thisIt's more. I want to know why you use it.

Oh, and when you print a string like this, you don't have to put the & sign in front of the variable name like scanf("%s", q) but why?

c char

2022-09-20 19:59

1 Answers

First of all, the array and the pointer are different.

(If you don't know what an array is after a variable's name, look at the basic book again and read this article.)

char * is a pointer to the address of the variable in which the character is stored. It's not the same as the arrangement.)

In the declaration and initialization phase, let's say char * p = "hello"; Here, since the string hello is string literal, the change is not possible. Simply put, it is not a value stored in a char array, but a string constant that is read-only and stored somewhere in memory.

But using a pointer p, you can approach the array and change the data.

    char q[100]="hello";
    char* p = q;
    printf("%c", *p);

Second, char q[100] is an array, so data input/output is possible. It's similar to putting a number in an int-type array and outputting it (not the same).)

Question 2.

And the scanf function is a function that is used to get a string input.

Write scanf("%s", q) because q is the name of the array.

If you think about what the name of the array means, you can guess the answer.


2022-09-20 19:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.