Character arrays and pointers - part 2
Vložit
- čas přidán 24. 04. 2013
- See complete series on pointers in C/C++ here:
• Pointers in C/C++
See part- 01 of this lesson here:
• Character arrays and p...
In this lesson, we have described how we can work with character arrays using pointers. Character arrays are used to store strings in C and we work with them for all kind of string manipulation. Working with string in C is tricky and requires good understanding of pointers.
Feel free to drop your comments. feedback and suggestions.
For practice problems and more, visit: www.mycodeschool.com
Like us on Facebook: / mycodeschool
Follow us on twitter: / mycodeschool
You are amazing! it is 2020 and these video series are by far the best in CZcams. I wish you could come back and start making more videos!
Exactly....Best explanation ever.......
He is dead.
@@entropy3387 his friend is dead. The one made the videos is alive and working in Google 🙌🏼😊
@@thestarinthesky_ Oh, my bad. Thanks for the heads up.
@Iker Donald this is a well known scam
I was so confused from Bucky Robert tutorials, then after reading a book about pointers I got more confused, now things are clear. Thank you best CS teacher :) where were you when things were complicated as fuck? you would save a lot of timeee
Best description of memory allocation and the usefulness of pointers I've ever seen. Absolutely superb lecture series. Thank you.
These are the best computer science videos on the internet.
by far, The best ever explanation for Tricky POINTER. Thank you for making it easy to grasp.
I'm new to C programming, but the way you explain things makes so much sense. Thanks!
You are absolutely genious! These are the most helpful videos about pointers I've seen... Thanks alot!!
Such an excellent job you did with all these videos! Thanks!
Best videoz ever on pointers man seriously, helped alot...Thank you...Thank you.
I loved that part sir
incrementing a pointer increments the address by the size of that data type.. awesome
Thank you sir, I've understood the concepts of pointers after a long time!
It is the irony that a prank video gets million of likes. But the informative video like this don't. Best video that explain the concept of any programming language (not limited to C/C++). Thanks a lot.
The truth is, only a minority part of Internet users are here to learn something, and the majority are just wandering here for entertainment.
Thank You So Much. Everything in my book and lectures makes so much more sense now.
Every other teacher irritates me, but not you. Thank you
thanks, sir, for the concept of string constant and constant pointer(array).
Superb video. Hats off to your dedication. This is the best video series for memory concepts.
pointers are wonderful you need just a person like you to explain it (Y)
*My takeaways:*
1. char* vs char[] 7:30
Great man, I like ur simplicity and clarity. Genius way of explanation.
the best demistifying pointers tutorial ever !!
Bro I love you so much, I did my OpSys homework, thanks to you
wow, what an amazing explaination, thank you so much!!
Excellent explanation. Thank you so much!
really helpful. He explained it really well and easily.
U are amazing .
Thank you so much for this playlist ❤️
Awesome explanation sir,you are the best ,make more video like that.thank you sir
i love your editing. good pace.
great explanation, very helpful tutorial, thanks a lot sir 👍👍
its working fine for me. I will have to see your code to help.
Bestt pointer tutorials yr....😊😊
This is the best..............Thanks Sir....
Excellent video
best video ever ... keep on going
your fan from iraq
They are really helping me building, although I am in high school 👌
i reckon we can use while(*c) only as well... !='\0' is not required as while loop will terminate when it encounters '\0', i.e 0, i.e null value.
That is so true. I didn't know it till I tried it. U can remove the printf statement to print a new line as well.
Best channel 👏👏🤗
Lovely.
Thank you
super helpful!!!!
Thank you for saving my degree.
its just awesome ...
tnx ...
you are so awesome , where have you been all my coding life :) :D
ikr? same question !
Can't call it a 'coding life', if you don't understand pointers.
You actually can. With languages that don't use pointers.
And yes, there is a point to not use pointers.
@@dansintean6820 Only Pussies who can't understand pointers and the languages that you talk about are using pointer in the back-end.
It should work. Its working for me. You are passing s to printf, right?
I like this tutorial almost bare metal programming
You are awesome 😍
great buddy
brilliant
super tutorial
@8:28 how the string will be stored as constant in the text segment?
In the olden C++ compilers(up through C++03) , for char const C[20]="HELLO"; const was implicit hence you didn't have to mention it . But in the new C++(from C++ 11) its not implicit(still works in C though),so remember to add const.
You are love
Did you just say thanks for watching??? When we should be the one thanking you. Awesome videos
So I wrote this code:
int main()
{
char c[5];
cin>>c;
cout
I have doubt at 8:15 how come it is possible for a pointer variable to act as array. Please tell in detail if possible.
So, it must be some error in your function.
thank you, you will be missed
2023 really helped, usually i am not commenting type, even tho this video is old yet still gold
Dear admin,why cant we change the c[0] in main function where else it could be changed in user defined function.Y it is so
in c++ .. I have a problem to have the adresse of elements of array char ... pliz help m
Sir you said in typical architecture pointers store 4 bytes of memory so when we are adding 1 and when the pointer is of char data type then how it's gonna add only just one byte of memory to itself as in a typical architecture you said pointer stores 4 byte of memory. So I get confused only in this explanation
Feb. 2023 - Best pointers explantation.
what is ide you are using?
please upload videos on STL.....
I am not getting any error when I tried to change the string literal assigned like char *a="Hello" and I modified it to a[0]='i';
You must have got an error, change your compilers and once try it on any online compiler, or try in this : www.codechef.com/ide
yeah!! you won't get any error, though when you print the string it is a null one.
it is the way a typical modern day compiler works..
best of luck!!!
someone please explain "const", what does it do?
char pointer would take the memory of one byte or 4 bytes in the stack frame ?
3:51
Pointers have a different size in bytes than their non-pointer datatypes. I'm using a 64 bit machine and the size of my character pointer is 8 bytes. It should be 4 bytes on a 32 bit machine.
better than my uni lecturer :)
Same. I understand a lot more from this channel's videos than my school.
So if you modify C in the print function then this wont affect C in main? This doesnt make sense because you are passing it the memory address of C. Please help.
it is because you passed the address (starting ) and it takes two pointers one in main function and one by referenced argument so if you modify increment pointer 2 it doesn't affect pointer 1
Wait, let me get this straight. You cannot say change:
Char *C = "Hello";
C[0] = 'A';
Because C is compiled as a String Constant and you cannot ever change String Constants, or is it that because it points to the starting address of "Hello", but you CANNOT CHANGE THE ADDRESS of "Hello" ?
It's because it is compiled as a string constant. And why not, you can change the adrress of "hello" you just can't modify its contents
I love how the video is exactly 10 mins
you are GOD
I am confused, i come to this example either I don’t fully understand pointer or something is missing
#include
void swap(char *str1, char *str2)
{
char *temp = str1;
str1 = str2;
str2 = temp;
}
int main()
{
char *str1 = "geeks";
char *str2 = "forgeeks";
swap(str1, str2);
printf("str1 is %s, str2 is %s", str1, str2);
getchar();
return 0;
}
And right solution is
#include
/* Swaps strings by swapping pointers */
void swap1(char **str1_ptr, char **str2_ptr)
{
char *temp = *str1_ptr;
*str1_ptr = *str2_ptr;
*str2_ptr = temp;
}
int main()
{
char *str1 = "geeks";
char *str2 = "forgeeks";
swap1(&str1, &str2);
printf("str1 is %s, str2 is %s", str1, str2);
getchar();
return 0;
}
Why we using double pointer if char pointer already have the address of string literal
at 3:35 he allocated memory for pointer from address 154,why he did not allocated it from address 150-154 , i am not getting it , plz anybody explain ???
best best
why does char * c stores string as a constant while char c[20] doesn't??
because char c[20] is a character array, so think of it as:
c[0] = 'h';
c[1] = 'e';
c[2] = 'l';
c[3] = 'l';
c[4] = '0';
c[5] = '\0';
and the rest is left blank for now.
However for char *c = "hello";
Char *C is COMPILED as a string constant by C compiler, and string constants cannot be changed. Another explanation according to my understanding is that Char *C is a pointer and points to AN ADDRESS, you cannot CHANGE the ADDRESS of something that is constant, in this case Char *C = "hello";
@@theamjolnir9641 you can certianly change the address but you won't be able to change its content.
P.S. i believe by now you know this but i couldn't help😅
great explanation of the concepts ... I have one question though ... can we declare
char *s = "Hello" and pass s into the print function....I am getting segmentation fault when i tried to do so ... can u please explain
Make sure the printf() function has the correct placeholder. %c for char and %s for string.
the C++ will do
because it will increase the address an print element on that address
so if we are in 32-bit arch then any address is 4 bytes , so when we point to char we have address of 4 bytes but each time we only increment by 1 then the next element after the first will have 4 bytes +1 to get to the next byte, in int we will have 4 bytes +4 to get to the next 4 bytes !!
and why if we declare int pointer to char it give compile error > we said that pointer is just address and it not difference unless we deference ,so its all 4 bytes is we use 32-arch
You confused Stack with Heap in its position in memory. Stack grows downward whereas Heap grows upward - this can cause confusion in the locations of variables and functions since they are located in the stack. A great series of instruction other than the error.
All these downwards and up words are just an human assumption.
If assigning string literals to char* makes them constant. Why do we use const char* while declaring a new string. Why not char* str = "Hello" which is also constant instead of const char* str = "Hello"
showing error on changing to char*c
warning: deprecated conversion from string constant to 'char*'
why?
put const before char.
Hey! The pointer *c of the user defined function should be taking 1 byte of memory right? it is a character pointer.. i guess you have made a slight mistake there... please check 3:40 to 3:50
character variable will take one bytes, but pointer to character will take 4 bytes. Pointer to any data type will be of same size because in pointer we store address of the first bytes of variable. Size of pointer should depend upon total number of addresses in memory and not on what data type it is. :)
You can see a pointer as an address that is represented by an integer. So if the pointer points to address 504 were a character is placed, and you increment the pointer, the pointer will point to address 505. And in that address the next character of the array is located.
I hope this helps?
Pointer is a variable that stores the address of the variable pointing to, so it stores an integer number, and since the size of an integer number is 4 bytes, the size of a pointer should be 4 bytes too.
how a character pointer variable can store a 4 byte integer address data?
Hello a pointer variable is a generic one,It will take same memory for any datatype.It is storing memory not datatype.So it will take 4 bytes in 32 bit and 8 bits in 64 bit
it is storing address not value REMEMBER
at 7:41 you changed C[20] ="Hello" to *C = "Hello" I got a build error within C++ compiler on my VS2017. ?? Been following your pointer playlist and everything worked until now. Error E0144 .I'll skip this part for now.
do pointers exist in other programming languages?
sure...
Yes, like C++ and in Java you can get something like "Null pointer exception." but you won't learn about them unless you really go in depth into Computer Science. C is kinda where all the languages thereafter evolved from.
Most of the other languages like Java have concept of "References" which are less confusing than concept of "Pointers" in C/C++.
8:20
So what's the point of char *s = "some string"? While i can use char s[ ] = "some string". What the use of that, faster, memory saving or something?
+Rock Madly When we use char* s we get a pointer that can point to a string of any length as compared to char s[] which has fixed length.
in char *s=//u can assign only single character
Rest in peace.........
no...to user defined function print
how char pointer store 4 bytes in memory ??? in- spite of it char store 1 byte only ???
Pointers have a different size in bytes than their non-pointer datatypes. I'm using a 64 bit machine and the size of my character pointer is 8 bytes. It should be 4 bytes on a 32 bit machine.
a char variable has size 1 byte because a char variable stores the character whereas a "pointer to char" stores the address of a variable which is of the type char. Note- On the same machine all the z'pointer to datatypez' will have same size i.e. a "pointer to int"(int* a) and "pointer to char"(char* d) will have the same size coz in any pointer we store the address....and address is a number.
But in last video u changes c[0] to A and printed AELLO. What was that>??
When he scaned HELLO
H got index 0,when he put c[0]='A' it changed to AELLO that is simple mate :)
Sir, how the pointer takes 4 bytes of memory, while it is of char type ?
Pointer stores address of the character type and address is always a integer, always keep in mind that the pointer is actually a other type of variable.
Pointer stores address of the character type and address is always a integer, always keep in mind that the pointer is actually a other type of variable.
The data type of the pointer is irrelevant. The main reason for why we can give it a different data type to a pointer is to improve readability
Sacred Lectures..
Why is character pointer stored in 4 bytes.It has too be stored in 1 byte then why is it so ??
2:46 it should be 100 -119
char *c="Hello";
printf(c) gives output=> Hello
printf(c+2) gives output=> llo
printf(*c) gives an output=> H
How to get the address? i.e in terms of 0x00...
&c does the job
why i am gettting "hello" instead of "Aello" as output?i copied the whole code still getting "hello" !
#include
#include
void print(char *c)
{
c[0] = 'a';
while(*c!='\0')
{
printf("%c",*c);
c++;
}
printf("
");
}
int main()
{
char c[20] = "hello";
printf(c);
}
see the second last line printf(c) . Change it to print(c). Cheers.
in your main you have "printf" it should be "print" method
can someone explain output of this programm?please
#include
void foo(int*);
int main()
{
int i = 10, *p = &i;
foo(p++);
}
void foo(int *p)
{
printf("%d
", *p);
}
why is it giving same o/p on using foo(p) and foo(p++)
p++ is reffered to next memory location along to i variable(memory stored). So, correct way to write this is foo(p), since p++ is considered unknown. But, since *p is pointing to an interger type, that means it is movig by 4 bytes, you are moving 2 or 4 bytes in forward and hence you are getting 10.
Because p++ is post increment so when you are using foo(p++); it is same as foo(p);. the value of p will be incremented afterwards.
What Aman Roy said, you call p then you increment p, and even if you call foo(p++) twice you probably won't get to what you think you'd be getting which is 11 being printed. This is because P is a pointer and when you increment a pointer it moves to point to another address in memory, therefore *p with the address of int i never goes on to increment i++ to 11.
Instead you would get something like this: (I recommend you trying this out in your own program compiler)
int i = 10;
int *a = &i;
foo(a);
printf("\t %d \t", *a);
printf("\t %d \t", a);
a++;
printf("\t %d \t", *a);
printf("\t %d \t", a);
output:
10 10 1485957944 0 1485957948
as you can see, foo(a) prints out &i no problem, and if you were to dereference the pointer *a to give you the value of i, that is also no problem, a would give you the actual address which is 1485957944 in this program, and then next you can see what happens when you a++ in main which is equivalent to your foo(p++) - you can see that *a now points to something else now, and that a becomes a new address that differs by 4 bytes, because most systems int pointers are 4 bytes.
To solve your problem and to increment i, you would need something like so: (*a)++, put *a into braces in main:
foo(a);
printf("\t %d \t", *a);
printf("\t %d \t", a);
(*a)++;
printf("\t %d \t", *a);
printf("\t %d \t", a);
like so, this has to do with precedence and associativity and pointer arithmetics - it'll give you an output like:
10 10 1528470328 11 1528470328
this time - observe that the address doesn't change, but the value of int i however is incremented by one. I hope this helps!
time 3 :21