Чому дорівнює char

0 Comments

Функція CHAR

У цій статті наведено синтаксис формули та описано, як у програмі Microsoft Excel використовувати функцію CHAR.

Опис

Ця функція повертає символ, визначений певним числом. За допомогою функції CHAR можна перетворювати номери кодових сторінок, отриманих зі створених на інших типах комп’ютерів файлів, на символи.

Операційне середовище

Набір символів

Набір символів Macintosh

Набір символів ANSI – це стандартний набір символів, які використовуються в операційних системах Windows через Windows 95 і Windows NT, після чого було прийнято Юнікод. ANSI складається з 218 символів, багато з яких мають такі самі числові коди, як і у форматах ASCII або Юнікод. https://www.techopedia.com/definition/932/ansi-character-set

Синтаксис

Синтаксис функції CHAR має такий аргумент:

    число – обов’язковий аргумент. Число між 1 і 255, яке визначає потрібний символ. Це символ із набору, який використовується на комп’ютері.

Примітка.: Інтернет-версія Excel підтримує лише CHAR(9), CHAR(10), CHAR(13) і CHAR(32) і вище.

Приклад

Скопіюйте дані прикладу з наведеної нижче таблиці та вставте їх у клітинку A1 нового аркуша Excel. Щоб відобразити результат обчислення формул, виберіть їх, натисніть клавішу F2, а потім – клавішу Enter. За потреби можна змінити ширину стовпців, щоб відобразити всі дані.

Відображає символ, представлений як 65 у наборі символів комп’ютера.

Відображає символ, представлений як 33 в наборі символів комп’ютера.

Difference between char* and char[]

The answers below pretty much explain it, but one additional difference is that the array variant gives you convenient way to take the size of the initial string (including the null terminator) – sizeof(str) .

8 Answers 8

Is an array of chars , initialized with the contents from “Test”, while

is a pointer to the literal (const) string “Test”.

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of “Test” , while the pointer simply refers to the contents of the string (which in this case is immutable).

141k 49 49 gold badges 206 206 silver badges 246 246 bronze badges
80.9k 20 20 gold badges 162 162 silver badges 169 169 bronze badges
Why is char* str commonly used when str denotes a string. Isn’t str[] better?

@Ajan: str[] is a statically sized array, which copies the string. If you need a copy -only needed if pretending to modify the string-, then the array is better. Otherwise a pointer to a literal string is preferable.

@Ajan: They do not both denote constant, string literals are allowed to bind to non const pointers just to support legacy C. Literals should always be refered by a const pointer to avoid potential undefined behavior.

Ok, that explains why I get warning: deprecated conversion from string constant to ‘char*’

@K-ballo if str is a pointer in char *str = “Test”; then does that mean that the literal “Test” is stored somewhere in the memory?

The diference is the STACK memory used.

For example when programming for microcontrollers where very little memory for the stack is allocated, makes a big difference.

char a[] = "string"; // the compiler puts onto STACK char *a = "string"; // the compiler puts just the pointer onto STACK // and in static memory area. 

15.5k 9 9 gold badges 65 65 silver badges 95 95 bronze badges

Does that apply only for microcontrollers? If not, then what’s the difference b/w static memory area and stack?

This would apply even if you weren’t programming for microcontrollers, but were concerned about memory usage. Microcontrollers normally have less memory for the stack than say your computer.

@dijonkitchen In the first case, the array of 7 characters takes up 7 bytes of memory whereas in the second case the pointer to a character takes up 8 bytes. Does this mean that the array of characters is more favorable over the pointer case for memory-critical usages? Moreover, in the pointer case the pointer takes up memory as well as <'s','t','r','i','n','g',0>.

I guess it would depend on what you’re pointing to. If it’s a string longer than 8 bytes, it’d use less memory with a pointer. And vice versa.

Upvote for this answer in the perspective of assembly language.

A pointer can be re-pointed to something else:

char foo[] = "foo"; char bar[] = "bar"; char *str = foo; // str points to 'f' str = bar; // Now str points to 'b' ++str; // Now str points to 'a' 

The last example of incrementing the pointer shows that you can easily iterate over the contents of a string, one element at a time.

20.7k 2 2 gold badges 66 66 silver badges 96 96 bronze badges

One is pointer and one is array. They are different type of data.

sizeof array 5 sizeof pointer 4 

10.4k 3 3 gold badges 35 35 silver badges 57 57 bronze badges

that is something new so when is stored in pointer that doesn’t need to be terminated by NULL character?

@Stack Overflow ponter is stored as adress that takes 4 bytes. Longer string would return as sizeof array 20 sizeof pointer 4

is an array of five characters, initialized with the value “Test” plus the null terminator ‘\0’ .

is a pointer to the memory location of the literal string “Test” .

141k 49 49 gold badges 206 206 silver badges 246 246 bronze badges

@PeterOlson you said pointer to the memory location. Does that mean ptr stores the starting address of the “Test”? One thing i know clearly pointers can only store address and “Test” is not a address.

@StackOverflow Yes, a pointer points to the starting address, and then you can find the string by going the the starting address (pointing to the letter T ) and going right until the null terminator is reached.

@PeterOlson Does that mean that the literal “Test” is stored somewhere in memory and terminated by ‘\0’ and ptr pointing to ‘T’ of “Test”? If yes, which memory does “Test” resides in?

Starting from C++11, the second expression is now invalid and must be written:

const char *str = "Test"; 

The relevant section of the standard is Appendix C section 1.1:

Change: String literals made const

The type of a string literal is changed from “array of char” to “array of const char.” The type of a char16_t string literal is changed from “array of some-integer-type” to “array of const char16_t.” The type of a char32_t string literal is changed from “array of some-integer-type” to “array of const char32_t.” The type of a wide string literal is changed from “array of wchar_t” to “array of const wchar_t.”

Rationale: This avoids calling an inappropriate overloaded function, which might expect to be able to modify its argument.

Effect on original feature: Change to semantics of well-defined feature.

21.8k 7 7 gold badges 40 40 silver badges 64 64 bronze badges

Suppose char* is the return type of a function char* fun() ? Will there be any change in the specification of the return type or the returned value now?

“Test” is an array of five characters (4 letters, plus the null terminator.

char str1[] = “Test”; creates that array of 5 characters, and names it str1 . You can modify the contents of that array as much as you like, e.g. str1[0] = ‘B’;

char *str2 = “Test”; creates that array of 5 characters, doesn’t name it, and also creates a pointer named str2 . It sets str2 to point at that array of 5 characters. You can follow the pointer to modify the array as much as you like, e.g. str2[0] = ‘B’; or *str2 = ‘B’; . You can even reassign that pointer to point someplace else, e.g. str2 = “other”; .

An array is the text in quotes. The pointer merely points at it. You can do a lot of similar things with each, but they are different:

char str_arr[] = "Test"; char *strp = "Test"; // modify str_arr[0] = 'B'; // ok, str_arr is now "Best" strp[0] = 'W'; // ok, strp now points at "West" *strp = 'L'; // ok, strp now points at "Lest" // point to another string char another[] = "another string"; str_arr = another; // compilation error. you cannot reassign an array strp = another; // ok, strp now points at "another string" // size std::cout 

for that last part, note that sizeof(strp) is going to vary based on architecture. On a 32-bit machine, it will be 4 bytes, on a 64-bit machine it will be 8 bytes.