String In Char Array VS. Pointer To String Literal | C Programming Tutorial

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Like the video? Check out these C programming playlists with hundreds of videos! 🙂

PortfolioCourses
Автор

A little bit more in depth explanation:

- In C string literals ( aka double quoted strings ) if not used as initializers for char arrays are staticilly allocated, that means they live in the .text / .rodata section and under most operating systems thats read only ( aka no self modifying code for you ;).
- char s1[] and char *s2 are different types, s1 can be used in functions expecting a char* parameter because of some evil compiler backwards compatibility magic which automatically converts any function(s1) -> function(&s1[0])
- Both s1 and s2 in the example is stack allocated, but while s1 is an array of chars all values are also stored on the stack, while s2 is a char pointer and only the pointer variable is stack allocated the value it points to isnt.

Its also quite interesting to look at the assembly your compiler generates and learn from that ;)

bayyyi
Автор

I have watched many videos teaching what pointer is and how to use, but only this one can make me truly understand how to use pointer in a few minutes.


This must be a good day for me, as I have learnt thing from a genius.

lamtatyan
Автор

This subscription reminder is the best I have seen so far. Totally non-obtrusive and non-annoying. I love it. *Klicks subscribe*

danielheinrich
Автор

Very good video, thanks to your calm, soothing voice and the low pace in which you explain everything step-by-step!

juuls
Автор

Excellent explanation! I just went through a string section in a C course I am taking and completely misunderstood the content. Even though I was able to stumble through a couple projects, I really did not understand what a "string literal" was and how it was different from a character array defined as char[]="some string". This cleared it up perfectly. You have a rare gift in instruction for the C language and I feel lucky to have found Porfolio Courses. Judging by the other comments, I hope you will continue this great work.

jongecsek
Автор

Portfolio courses is without a doubt, the best c programming / computer science teacher !!

mefrefgiweuhef
Автор

Extremely useful. Please make the same video with more examples and corner cases. You are very good teacher. You deserved to be paid.

komalshah
Автор

char *s is precompiled in .text while char s[] is allocated in .data section
so if you try to modify *s like s[0] = xxx, it will usually cause a sigseg since you're modifying .text section which is read only

rkwpmhf
Автор

Great video. It's been a couple decades since I've done any C programming and I've forgotten some details like this since my programming focus has shifted considerably to functional programming, Python, and C++.

vorpal
Автор

Litterally had the exact same question a week ago in my HPC-course! Many thanks 🙏🙏🙏

gabrielshafiqahlgren
Автор

In case of embedded devices, and so I suppose in desktop application case also:

char *s = "abcdef";
"abcdef" goes together with other constant variables to RO (Read Only) section of memory, that is a part of FLASH (CODE + RO).
s pointer goes together with other non-constant global/static variables to RW (Read/Write) section of memory, that is a part of RAM (RW + ZI (Zero-Initialized)) (technically it placed in FLASH, but loads to RAM during initialization).

char s[] = "abcdef";
"abcdef" goes to RW section ("s[]" goes nowhere, it kinda does not exist unlike "*s", that is specifically variable type to store address pointing to another location, because all address calls to "s[]" are stored in code commands and cannot be modified and it's the reason why you can't reassign "s[]" to another string later in the code).

That's why if you want to save RAM space when using a lot of string arrays those are not gonna be modified, you must declare them "const char s[]" so their values could be placed in RO section (in this case it may look the same as "char *s", but there are still differences in compilation and how it processed by different functions like sizeof(), because array will have data type "char [<size>]" not a pointer type).

This explains why you get that access error trying to modify value that is placed in RO section.

rokko
Автор

Good video, but I like to also add that char msg[] ="hello";
is just shorthand for
char msg[] ={'h', 'e', 'l', 'l', 'o', '\0'};
There are no such things as strings
in C. Only char arrays or
character arrays. And the address of first character of the char array is
fixed, it is a constant pointer, &msg[0] and msg mean the same thing. And msg and
msg + 0 is also the same, you can't change the base address of the array, it is suck in memory. It makes
sense, lets say you had this :
char s[]="car";
then decided to change it to
s =
;
The compiler would have to find a large amount of memory for this new change in size.
And there might not be enough memory for this new sudden change in size. Therefore a array's
starting address being fixed is a good safeguard.
Therefore the base address of any
array in C is fixed, it is a constant.
This applys to all arrays in C, for example:
int a[]={1, 2, 3};
int b[={4, 5, 6};
you can't do this
a= b; same as a= &b[0] ;
// because you are changing the starting or base address of the integer array called a . The address of a is fixed, and will remain fixed for the entire program.
But I can do this,
int *p = a; int *p =&a[0];
then later I can change the int pointer to point to array b
p = b; because p is a
pointer variable that can point to any variable or any array as long as it has the same data type, as the variable or array, that it is pointing, or refering to. Pointer variables can point to any variable of the same type. Again as I said earlier, char arrays or C-strings are character arrays only , there is no such thing as a string in C, they are not String objects like Java, or String objects like C++ or C# .

rickzalman
Автор

Once again, a clear explanation on a tricky point. Thanks 🙏

logos_
Автор

Yep, depends on your compiler. I've lived with this over the years I've been working with C on embedded systems. It's one reason why fixing your projects code development to a particular version of your compiler tool chain, AND the optimisations you use, is very important.

mikehibbett
Автор

One is a string, initialised with abcdef and the other is a pointer to part of the program itself, 6 bytes representing abcdef. When I was writing embedded process control software a million years ago, the first version would be 6 bytes of RAM and the second would be a pointer in RAM pointing at a string of 6 bytes in ROM.

occamraiser
Автор

Very clear and good information! Thanks !!

DarylStark
Автор

great channel i'm planning to see your whole c++ course later 👍

justcurious
Автор

Awesome stuff. Learned something new. Thank you

amanimavu
Автор

When I'm building code with a new toolchain I am always looking at where that tool is putting my string literals (through the map file) just to make sure I understand what it's assumptions are. And these assumptions vary with optimisation options

mikehibbett
visit shbcf.ru