Character arrays and pointers - part 1

  Рет қаралды 551,618

mycodeschool

mycodeschool

Күн бұрын

Пікірлер: 327
@eriaardwen946
@eriaardwen946 8 жыл бұрын
The code at 14:30 works because the character array C is stored in a contiguous block of memory, so when we increased the pointer C, we were traversed through the character array C by one byte at a time, which is the amount of memory required to store a character. Once the pointer reaches the null terminator, the loop ends.
@eduardusharditjiputra6506
@eduardusharditjiputra6506 4 жыл бұрын
@@akashsahu933 did you even read the rest of the explanation?
@amansinghbhadauria2818
@amansinghbhadauria2818 4 жыл бұрын
@@akashsahu933 SHUT UP!
@sagivalia5041
@sagivalia5041 3 жыл бұрын
Good to note that it also works with an array of integers since the incrementation automatically increments it by 4 bytes, which I find to be extremely cool.
@thisaintmyrealname1
@thisaintmyrealname1 4 жыл бұрын
This whole channel is gold. Thanks. I'm watching this as I read the K&R C book.
@keen2461
@keen2461 3 жыл бұрын
This guy explains really well. I have a full book on pointers, but I prefer to watch his videos. Awesome C course.
@palashbehra9303
@palashbehra9303 3 жыл бұрын
can i know which book it is? also, is it any good?
@keen2461
@keen2461 3 жыл бұрын
@@palashbehra9303 The book is "Understanding pointers in C" by Yashavant Kanetkar. I bought it in India while on a business trip there.
@palashbehra9303
@palashbehra9303 3 жыл бұрын
@@keen2461 thanks man
@asterisk2598
@asterisk2598 Жыл бұрын
@@palashbehra9303 I used Let Us C and it is pretty good too
@naboulsikhalid7763
@naboulsikhalid7763 4 ай бұрын
I see everybody is benefiting from your teaching, and I will thank you for doing so. Great simple concise and complete explanation. Thank you
@harunwagura4727
@harunwagura4727 4 жыл бұрын
14:30 works because: Since the array is passed by reference to the function, the pointer will point to the base of the array which is index 0. Then because C is passed as a char pointer, incrementing C by one will ensure traversing the array char after char since a char is 1 byte.
@mycodeschool
@mycodeschool 11 жыл бұрын
Hi HashDLS, Size of pointer variable will always be same whether its a pointer to int or pointer to char. Its an address right. So, It's about how many bytes we want to use to store an address. Now, this could be address of anything. In 2 bytes or 16 bits we can store max 2^16 addresses, in 4 bytes or 32 bits - 2^32. It depends upon compiler and architecture of machine. Turbo -C may be compiling for 16 bit architecture. CodeBlocks would be compiling for 32 bit architecture.
@Tahycoon
@Tahycoon 2 жыл бұрын
Hi!
@GKT196
@GKT196 4 жыл бұрын
Sir you did really great job to demonstrated all concepts in very lucid way, you are rally adept,veteran teacher who delivered such complex topics in such a way to ease way to understandable
@yashpandey9550
@yashpandey9550 8 жыл бұрын
at 9:31 you said that print C2[1] will give us value l ???? But shouldn't it give us the value e ?
@slays6916
@slays6916 8 жыл бұрын
+Yash Pandey Yes, we should get e. Honest mistake
@saisrikar7987
@saisrikar7987 8 жыл бұрын
Yash Pandey hey guys... the value is'e' only.... because in c1 the values to"hello" will be given as h=0,e=1,l=2,l=3,o=4... So when c2[1 ] is given it gives 'e'.. and as *c2=c1......c2 will be same as character string c1.. So c2=c1... hope u understand...... reply me if u have any kind of doubts regarding that...
@dhirajkumargupta4151
@dhirajkumargupta4151 6 жыл бұрын
sai srikar lol😁 extra marks for steps 👍
@yanalashivaprasadreddy3010
@yanalashivaprasadreddy3010 5 жыл бұрын
it is a mistake.
@baderbboukheit3604
@baderbboukheit3604 5 жыл бұрын
I like the time difference between the replies , I love how knowledge unites people from not only different places , but different time :)
@kaino1990
@kaino1990 6 жыл бұрын
I dont usually write stuff like this but your video has helped explain alot that ive had trouble with at uni. Wish I had learnt this earlier but better late then never, thanks heaps and great video
@ozzyfromspace
@ozzyfromspace 5 жыл бұрын
For the question at the end, I think it works because the function print(char* C) took a pointer as argument. Well, pointers can be incremented, so starting at the initial address of the string allows us to locate all the characters because they are stored contiguously in memory. Is this right? Thank you.
@akhil1273
@akhil1273 4 жыл бұрын
exactly
@chandranshpandey1929
@chandranshpandey1929 3 жыл бұрын
i am thinking the same
@kirannaik4417
@kirannaik4417 2 ай бұрын
Yupp, you're correct.
@mohamedatef3526
@mohamedatef3526 5 жыл бұрын
This video didn't become the top viewed in this subject for nothing . Thank you sir
@akhilgeorgejimmy240
@akhilgeorgejimmy240 2 ай бұрын
this channel is just amazing, up there with the best cs channels in the world
@sebyandrisan4750
@sebyandrisan4750 5 жыл бұрын
Your videos saved my exams at university Thank you from Greece!
@JudyAbbott494
@JudyAbbott494 4 жыл бұрын
Well it saved you not your exams lol
@ceechi2408
@ceechi2408 9 жыл бұрын
@mycodeschool ur tutorials are simply the best. thx a lot
@alirezaghodsipoor2239
@alirezaghodsipoor2239 5 жыл бұрын
Awesome! God bless you. Good example when you first didn't insert and then inserted null character at the end of string.
@chriswesley594
@chriswesley594 8 жыл бұрын
You have made a superb video - many thanks.
@Fikri_A777
@Fikri_A777 4 жыл бұрын
Seriously helpful for me to understand this "array" thingy, thanks dude for the video
@azizbekdavlatogli3371
@azizbekdavlatogli3371 8 жыл бұрын
9:24 why c2[1] is "l"? maybe it should be "e", as the second element of "hello"
@tejendrasinghtomar8175
@tejendrasinghtomar8175 8 жыл бұрын
indeed...that seems to be a mistake
@kareemayman1926
@kareemayman1926 5 жыл бұрын
Yep you are right that was a mistake
@manmohanreddy4914
@manmohanreddy4914 4 жыл бұрын
yes it is "e", not "l"
@mangizzle1272
@mangizzle1272 4 жыл бұрын
typing error.. you have much concentration.nice
@nathamuni9435
@nathamuni9435 Жыл бұрын
OO man , this man is doing magic even after 10 years
@carolinagordillo3307
@carolinagordillo3307 6 жыл бұрын
you are such a great teacher, thanks a lot! your video helped me hugely!
@SmartProgramming
@SmartProgramming 6 жыл бұрын
awesome work sir, really appreciating, thank you 👍👍
@Unknown-Stranger
@Unknown-Stranger 3 жыл бұрын
are Deepak sir
@alyshawky9918
@alyshawky9918 4 жыл бұрын
Many many many thanks .... You was very helpfull for me ... Your way in explaining things is more than excellant .... Thank you
@happyholi6328
@happyholi6328 6 жыл бұрын
Thank you very much admin you taught really classical
@precisionchoker
@precisionchoker 5 жыл бұрын
Thanks for the `while` idea, solved a problem I was suffering from.
@tomasz-rozanski
@tomasz-rozanski 7 жыл бұрын
You can also implement print() function like this: void print(char *s) { while (*s) printf("%c", *s++); printf(" "); }
@shivarajpatil162
@shivarajpatil162 7 жыл бұрын
Please explain this print function.. as am not able to understand this.
@TheGBsXB
@TheGBsXB 6 жыл бұрын
Or like this: void print(char *c){ putchar(*c++); if(*c) print(c); }
@lordstark5292
@lordstark5292 5 жыл бұрын
@@shivarajpatil162 or like this void print(char *c) { int i = 0; while(c[i]) { write(1, &c[i], 1); i++; }
@oliverbeck6839
@oliverbeck6839 4 жыл бұрын
wow very concise, well done
@cafarellano9340
@cafarellano9340 4 жыл бұрын
@@lordstark5292 That´s wrong as it would output the addresses of each individual character in the string. This is because you used the address of operator instead of the dereference operator.
@leixun
@leixun 4 жыл бұрын
*My takeaways:* 1. How to store strings 0:59 2. Arrays and pointers are different types that are used in a similar manner 7:38 3. Arrays are always passed to function by reference 11:20
@steveokinevo
@steveokinevo 10 жыл бұрын
These vidz are fantastic thanks for your time creating and uploading them...
@bocanica1
@bocanica1 9 жыл бұрын
Thanks for your tutorial! You're the best!
@sayantaniguha8519
@sayantaniguha8519 4 жыл бұрын
8:49 Then it can be *c1+i* also(executed inside a for-loop of i) where, i is the index of the alphabets of the string starting from 0
@sahareliyahu3462
@sahareliyahu3462 5 жыл бұрын
Great video's series on pointers. You make it very clear. thank you!
@afafemad187
@afafemad187 Жыл бұрын
When passing an array to a function, a pointer variable is created by compiler that points at the address of the first element of the array, thus, c (the parameter in print function) is converted to a pointer which points to the address of 'H'. So as pointers can be incremented (to point to the next location) AND array elements are stored contiguous in memory, the code works :)
@tarunkamineni92
@tarunkamineni92 10 жыл бұрын
thanks it helped me in passing character array as argument
@ismoiljonabdumajidov9260
@ismoiljonabdumajidov9260 2 жыл бұрын
thank you for the great lesson . i searched these information from you tubel a lot.
@sanchitchakraborty8146
@sanchitchakraborty8146 7 жыл бұрын
these videos are great sir.. i am recommending this channel to my friends and teacher..
@prashantdhawase8057
@prashantdhawase8057 3 жыл бұрын
Very beautiful handwriting ... Nice explanation too
@simonribas4625
@simonribas4625 3 жыл бұрын
thank you for the time you take to make these amazing videos!
@abhishekrai4325
@abhishekrai4325 9 жыл бұрын
brilliant ! Very easy and efficient explanations. Thank you so much sir.
@govindrathi2940
@govindrathi2940 6 жыл бұрын
At a point in this video he stated that declaring and intiliazing ( char A[4] = "JOHN" ) will give a compilation error but that doesnt happen. I checked it on two IDE,s GEEKSFORGEEKS and DEVC++
@danhle7999
@danhle7999 4 жыл бұрын
same here i dont know why? have you found out yet?
@mahendraaanjna2005
@mahendraaanjna2005 2 жыл бұрын
one thing to remenber is when using *(C+i) it is actually doing *(C+i*1) where 1 is size of char in bytes. In case of working with other type of arrays like int(4 byte) double(8 byte) we need to multiply explicitly
@mihai5085
@mihai5085 2 жыл бұрын
as far as i know, when you increment an pointer by 1 it jumps to the next element in the array, if an int is 4 bytes , the pointer jumps 4 bytes to the next integer by incrementing the pointer by 1. maybe you are using some language that doesn't?
@asterisk2598
@asterisk2598 Жыл бұрын
@@mihai5085 you are correct
@Haris-bg4jy
@Haris-bg4jy Жыл бұрын
@@mihai5085 It does and that's what the comment is explaining. That when you do *(C + i) the compiler actually reads it as *( C + i * sizeof() ) which is how it jumps to the next element instead of the next byte.
@hirokaanf4287
@hirokaanf4287 10 жыл бұрын
I believe that in a list the null terminated character is implicitly declared ?
@DarkLevis
@DarkLevis 8 жыл бұрын
Excellent videos in general, the best I've seen so far.
@francisfraa
@francisfraa 9 жыл бұрын
Amazing Video which is mastered by your hard work Kudos!!!!
@vinayvinay3031
@vinayvinay3031 2 жыл бұрын
Thank you for clearing my doubts
@variancaesar4778
@variancaesar4778 4 жыл бұрын
at 14:30 if I print the value of C after the print function called, does the memory address changed as well? (because we increment it inside the loop in print function)
@Dk35840
@Dk35840 7 жыл бұрын
Thanks for the video it clear my concepts , there is some mistake you had said forward slash at 3:34 but you had actually used backward slash so please correct it as the other user does not get wrong undrstnding
@jixuancheng
@jixuancheng 5 жыл бұрын
I think your videos are awesome. But in this video, I want to point one tiny thing, I don't think it matters, but print(char *C), it is passing arguments by value(not reference). In the book P.209 "Pointers behave like any other nonreference type. When we copy a pointer, the value of the pointer is copied. After the copy, the two pointers are distinct. However, a pointer also gives us indirect access to the object to which that pointer points. We can change the value of that object by assigning through the pointer" After I read this, I realized C/C++ has so many things need to pay attention to.
@ritadeeproy996
@ritadeeproy996 10 жыл бұрын
u really worked hard. thank u for uploading. it really helped me a lot. i can use pointer like a expert.
@sunnyrauniyar6769
@sunnyrauniyar6769 5 жыл бұрын
thankyou so much, your videos are so convincing :-)
@877dev3
@877dev3 5 жыл бұрын
Excellent video with the best explanation I have seen yet, thank you!! Plus leaving us homework :)
@mizanurrahmankhan692
@mizanurrahmankhan692 9 жыл бұрын
very nice tutorial. thanks a lot.
@vishnuvardhan-dp4zc
@vishnuvardhan-dp4zc 7 жыл бұрын
And. We loved this explanation!👌
@samdavepollard
@samdavepollard 4 жыл бұрын
these videos are gold
@suyogchavan8779
@suyogchavan8779 4 жыл бұрын
this may help me for passing this part
@muckymcfly
@muckymcfly 5 жыл бұрын
Who's here in 2019?
@baderbboukheit3604
@baderbboukheit3604 5 жыл бұрын
Where did he go ???
@adolfocarrillo248
@adolfocarrillo248 5 жыл бұрын
@@baderbboukheit3604 he´s in the other side.
@dahuangll
@dahuangll 5 жыл бұрын
@@adolfocarrillo248 it will be 2020
@rittenbrake1613
@rittenbrake1613 4 жыл бұрын
here in 2020
@awesomeps10
@awesomeps10 4 жыл бұрын
@Señor Mike no he didn't
@nathanbivens2394
@nathanbivens2394 5 жыл бұрын
When printing c2[1], why is that printing out a letter? If c2 is a pointer to the address of the first character of c1, wouldn't: print c2[1]; print out 201 (the address of the second element of the array)? I could see how running: print *c2[1]; would give you 'e', but that's not what's written.
@rizwanmushtaq15
@rizwanmushtaq15 6 жыл бұрын
Really Awesome, great and very clear understanding of the concepts! Thank you very much!
@carbonmc3782
@carbonmc3782 4 ай бұрын
Wonderful. You’re so awesome.
@Varrian839
@Varrian839 8 жыл бұрын
Thank you very much! I finally find my bug after watching it.
@kevinspacey6090
@kevinspacey6090 5 жыл бұрын
Didn't use \0 and it still worked.
@Al-nj6ww
@Al-nj6ww 5 жыл бұрын
Yes, it’s possible it may work. Hence the name undefined. But if you keep doing it, you will eventually run into problems. You can drive on the wrong side of the road and be fine, keep doing it and you’re going to run into some big issues.
@staticinteger
@staticinteger 10 жыл бұрын
Amazing video!
@raresraulea5455
@raresraulea5455 5 жыл бұрын
Hi! After declaring C1 as character array and C2 as pointer to character, is C2 = C1 somehow the same as C2 = &C1[0] and the compiler does know that now C2 is usable as an array? (since we havent declared it with [] anywhere). Thanks!
@nikolajkapa2283
@nikolajkapa2283 7 жыл бұрын
Good quality and depth.
@yellowtinted
@yellowtinted 3 жыл бұрын
thanks for your explaination!
@rakeshkandula2318
@rakeshkandula2318 5 жыл бұрын
Increementing of C works because. in print function it is reference value which can be incremented
@rajupdl6
@rajupdl6 6 жыл бұрын
Awesome video for beginner like me ...
@pavansai709
@pavansai709 9 жыл бұрын
how did" print c2[1] "give the output "l"? isnt it "e" ?
@mechaadi
@mechaadi 8 жыл бұрын
he mentioned in the correction.
@rushikesh4790
@rushikesh4790 6 жыл бұрын
Yup
@aperson4051
@aperson4051 5 жыл бұрын
If you increment the c pointer as in the last example. Do you need rewind it back to the original position, else it forever points at the newly incremented address?
@eur0dad
@eur0dad 11 жыл бұрын
your videos are amazing
@MrGentlemanism
@MrGentlemanism 7 жыл бұрын
C++ will work, because you are incrementing the start address that was given as a parameter!
@dognip
@dognip 2 жыл бұрын
This is gold.
@rikkoo
@rikkoo 2 жыл бұрын
the last exercise could be done this way while(*C) putchar(*C++); putchar(' '); if you dereference of the char pointer it will return the current character that is not zero so the loop will continue until it finds the null terminator.
@menderelcapbig1876
@menderelcapbig1876 3 жыл бұрын
perfect explanation
@totasalam7060
@totasalam7060 10 жыл бұрын
thank u very much you are simply amazing
@zarrafsharif3099
@zarrafsharif3099 6 жыл бұрын
How does printing the string of character array with %s format work since arrays point to the first element of the array??How does the array name for the string go to all the different addresses of the elements??
@boliussa
@boliussa 9 жыл бұрын
At 3:32 that's a backslash not a forwardslash. You can remember ti's a backslash because imagine a person walking across the screen.left to right. but he is falling backwards. Generally right is forward and left is backward.. Look where the top of the backslash is, it's back . It is like the slash that Windows command shell uses, CD BACKSLASH CD \, as opposed to the forward slash that linux command shell uses.
@AnToxa57
@AnToxa57 4 жыл бұрын
You really helped me out a lot!! Thank You!
@muhammadshoaibfareed2577
@muhammadshoaibfareed2577 5 жыл бұрын
very good video .You explain it beautifully . Other series kb ay gi ???
@kimnjklmnjhh1851
@kimnjklmnjhh1851 6 жыл бұрын
These videoes really help me. why my compiler shows the whole char array instead of prints out the first character of the array when i use cout to print C2?
@rootdefault6263
@rootdefault6263 4 жыл бұрын
is it possible to declare a character array that will accept input without placing size to make it a variable size array or you have to explicitly set the size. by that i mean there is no initialization just declaration of it. so it can be used later on as the program executes to take user input
@SAGARKUMAR-ec6go
@SAGARKUMAR-ec6go 5 жыл бұрын
u r amazing sir!!
@sanambaloch2975
@sanambaloch2975 4 жыл бұрын
no he is not amazing......he made a lot of mistakes
@AjaySharma-le3df
@AjaySharma-le3df 4 жыл бұрын
@@sanambaloch2975 like to share those mistakes?
@garhwaop
@garhwaop 11 ай бұрын
It is worth it to watch in 2024 ??? , ima beginner in c , i even don't have idea does any code or way of typing of code is changed . Should I follow your code ? Will it work ? For mine
@bariswheel
@bariswheel 2 жыл бұрын
In the last example I believe it will also work if the while loop was written : while (*C) . Meaning while *C is true , and it can only be true if it’s not null, is that correct? I suppose we can put it to the test.
@danhle7999
@danhle7999 4 жыл бұрын
at 4:21 it actually works on my code::block so what is the problem?
@perlecatt9123
@perlecatt9123 9 ай бұрын
bro i love u for this thanks
@ashmitgangwar5595
@ashmitgangwar5595 8 жыл бұрын
I Like Your Voice..! :p
@mohammedfawaz289
@mohammedfawaz289 8 жыл бұрын
his voice is calm and it makes you feel he is very confident :)
@ShawnDypxz
@ShawnDypxz 5 жыл бұрын
Because C is a pointer to a character. And C++ means to increment the value of C by 1 byte (the address)
@animeshsrivastava7113
@animeshsrivastava7113 3 жыл бұрын
A character array doesn't require null termination but if a character array needs to define a string then we add '\0' there to say that it's null terminated and now it is a string, so we can have char array without null termination but then don't expect to use it in strlen() or other functions which expect a string. Peace
@tingbinhuang4562
@tingbinhuang4562 8 жыл бұрын
amazing explaining, thank you :)
@aravindbabu2643
@aravindbabu2643 8 жыл бұрын
sir we can only icrement pointer variable only when it is assigned to other but how can we increment c.?
@gouthamyalamanchili4528
@gouthamyalamanchili4528 7 жыл бұрын
at 3:52 does it apply for c++???
@sammanirox
@sammanirox 10 жыл бұрын
Thanks for the video.Can you please tell me why this code `char a[25]="Hello World"; cout
@Zendo-v8k
@Zendo-v8k 9 жыл бұрын
sammanirox cout has a bunch of operators chosen at compile time. Here the compiler chose to have cout print your array as a string based on the fact that you sent the char array's first index memory address. You can "fool" the compiler to have cout print the memory address of the first char index by typecasting the pointer to (void *) instead. cout
@Shelly888-s1r
@Shelly888-s1r 4 жыл бұрын
The code at the end will work...coz when u wrote their *c...u derefrenced a value..named c...in the loop...u printed that derefrenced value....as for the fact that arrays are stored as a reference value in another function and only stores the address of the first variable....now after printing the first variable by drefrencing the address of it ...u incremented it...and in loop u passed the condition not equals to null...so it will terminate their and will give us hello as output...
@Shubham-ny2ce
@Shubham-ny2ce 7 жыл бұрын
Good to see such videos....
@vivekkoul4428
@vivekkoul4428 3 жыл бұрын
I have one doubt here. When he uses printf("%s", c) shouldn't memory address of c[0] be printed instead of JOHN? As he explained in the lecture on pointers and arrays that of we weite just the name of the string , then it will print the address of the first item of array.
@ozzmanr
@ozzmanr 8 жыл бұрын
very well handed do you have any youtube resources for c++ beginners covering the technical under the hood stuff, that dont just superficially walking you through writing a program?
@-ADivyasri
@-ADivyasri 4 жыл бұрын
Sir if u remove print c in the main function it works fine but coming to out it prints ntg .when put print c in the main it prints same as u told without that statement ntg is printed the function is returning ntg even if I kept printf inthe function
@thestarinthesky_
@thestarinthesky_ 4 жыл бұрын
@3:31 it is a backward slash "\" not forward slash :)
@asifrayan2420
@asifrayan2420 7 жыл бұрын
amazing videos sir.
@mostafayasin1
@mostafayasin1 5 жыл бұрын
Many thanks
@shibah9345
@shibah9345 3 жыл бұрын
Could anyone please help out, why don't we need loops to print strings as we would normally do with arrays of other data types
Character arrays and pointers - part 2
10:01
mycodeschool
Рет қаралды 273 М.
Pointers and 2-D arrays
12:19
mycodeschool
Рет қаралды 487 М.
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 28 МЛН
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 16 МЛН
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 334 М.
Pointers and multidimensional arrays
16:45
mycodeschool
Рет қаралды 246 М.
Pointers and arrays
8:43
mycodeschool
Рет қаралды 651 М.
Function pointers and callbacks
15:19
mycodeschool
Рет қаралды 337 М.
Function Pointers in C / C++
11:57
mycodeschool
Рет қаралды 439 М.
Pointers as function arguments - call by reference
14:16
mycodeschool
Рет қаралды 489 М.
Pointers and dynamic memory - stack vs heap
17:26
mycodeschool
Рет қаралды 1,5 МЛН
struct Basics | C Programming Tutorial
24:44
Portfolio Courses
Рет қаралды 154 М.
How Strings Work in C++ (and how to use them)
19:26
The Cherno
Рет қаралды 471 М.
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 28 МЛН