Character arrays and pointers - part 1

Поділитися
Вставка
  • Опубліковано 9 лют 2025

КОМЕНТАРІ • 327

  • @keen2461
    @keen2461 4 роки тому +60

    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 роки тому +1

      can i know which book it is? also, is it any good?

    • @keen2461
      @keen2461 3 роки тому +2

      @@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 роки тому +2

      @@keen2461 thanks man

    • @asterisk2598
      @asterisk2598 Рік тому +1

      @@palashbehra9303 I used Let Us C and it is pretty good too

  • @thisaintmyrealname1
    @thisaintmyrealname1 4 роки тому +19

    This whole channel is gold. Thanks. I'm watching this as I read the K&R C book.

  • @naboulsikhalid7763
    @naboulsikhalid7763 6 місяців тому +1

    I see everybody is benefiting from your teaching, and I will thank you for doing so. Great simple concise and complete explanation. Thank you

  • @eriaardwen946
    @eriaardwen946 8 років тому +80

    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 роки тому +3

      @@akashsahu933 did you even read the rest of the explanation?

    • @amansinghbhadauria2818
      @amansinghbhadauria2818 4 роки тому

      @@akashsahu933 SHUT UP!

    • @sagivalia5041
      @sagivalia5041 3 роки тому +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.

  • @GKT196
    @GKT196 5 років тому +6

    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

  • @harunwagura4727
    @harunwagura4727 4 роки тому +5

    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.

  • @akhilgeorgejimmy240
    @akhilgeorgejimmy240 4 місяці тому

    this channel is just amazing, up there with the best cs channels in the world

  • @mohamedatef3526
    @mohamedatef3526 5 років тому +1

    This video didn't become the top viewed in this subject for nothing . Thank you sir

  • @kaino1990
    @kaino1990 6 років тому +2

    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

  • @sebyandrisan4750
    @sebyandrisan4750 6 років тому +3

    Your videos saved my exams at university
    Thank you from Greece!

    • @JudyAbbott494
      @JudyAbbott494 4 роки тому

      Well it saved you not your exams lol

  • @nathamuni9435
    @nathamuni9435 Рік тому

    OO man , this man is doing magic even after 10 years

  • @mycodeschool
    @mycodeschool  11 років тому +21

    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.

  • @chriswesley594
    @chriswesley594 8 років тому +25

    You have made a superb video - many thanks.

  • @ceechi2408
    @ceechi2408 9 років тому +1

    @mycodeschool ur tutorials are simply the best. thx a lot

  • @Fikri_A777
    @Fikri_A777 4 роки тому +1

    Seriously helpful for me to understand this "array" thingy, thanks dude for the video

  • @SmartProgramming
    @SmartProgramming 6 років тому +4

    awesome work sir, really appreciating, thank you 👍👍

  • @carolinagordillo3307
    @carolinagordillo3307 6 років тому +2

    you are such a great teacher, thanks a lot! your video helped me hugely!

  • @alirezaghodsipoor2239
    @alirezaghodsipoor2239 6 років тому +1

    Awesome! God bless you. Good example when you first didn't insert and then inserted null character at the end of string.

  • @alyshawky9918
    @alyshawky9918 4 роки тому

    Many many many thanks .... You was very helpfull for me ... Your way in explaining things is more than excellant .... Thank you

  • @yashpandey9550
    @yashpandey9550 9 років тому +138

    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 років тому +10

      +Yash Pandey Yes, we should get e. Honest mistake

    • @saisrikar7987
      @saisrikar7987 8 років тому +3

      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 7 років тому +5

      sai srikar lol😁 extra marks for steps 👍

    • @yanalashivaprasadreddy3010
      @yanalashivaprasadreddy3010 5 років тому

      it is a mistake.

    • @baderbboukheit3604
      @baderbboukheit3604 5 років тому +33

      I like the time difference between the replies , I love how knowledge unites people from not only different places , but different time :)

  • @happyholi6328
    @happyholi6328 7 років тому +3

    Thank you very much admin you taught really classical

  • @steveokinevo
    @steveokinevo 10 років тому +5

    These vidz are fantastic thanks for your time creating and uploading them...

  • @ismoiljonabdumajidov9260
    @ismoiljonabdumajidov9260 2 роки тому

    thank you for the great lesson . i searched these information from you tubel a lot.

  • @simonribas4625
    @simonribas4625 3 роки тому +1

    thank you for the time you take to make these amazing videos!

  • @sahareliyahu3462
    @sahareliyahu3462 6 років тому +1

    Great video's series on pointers. You make it very clear.
    thank you!

  • @ozzyfromspace
    @ozzyfromspace 6 років тому +33

    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.

  • @precisionchoker
    @precisionchoker 6 років тому +1

    Thanks for the `while` idea, solved a problem I was suffering from.

  • @bocanica1
    @bocanica1 9 років тому +2

    Thanks for your tutorial! You're the best!

  • @vinayvinay3031
    @vinayvinay3031 3 роки тому

    Thank you for clearing my doubts

  • @ritadeeproy996
    @ritadeeproy996 10 років тому +10

    u really worked hard. thank u for uploading. it really helped me a lot.
    i can use pointer like a expert.

  • @sanchitchakraborty8146
    @sanchitchakraborty8146 7 років тому

    these videos are great sir.. i am recommending this channel to my friends and teacher..

  • @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 :)

  • @877dev3
    @877dev3 5 років тому

    Excellent video with the best explanation I have seen yet, thank you!! Plus leaving us homework :)

  • @prashantdhawase8057
    @prashantdhawase8057 3 роки тому

    Very beautiful handwriting ... Nice explanation too

  • @DarkLevis
    @DarkLevis 8 років тому

    Excellent videos in general, the best I've seen so far.

  • @yellowtinted
    @yellowtinted 3 роки тому

    thanks for your explaination!

  • @samdavepollard
    @samdavepollard 4 роки тому

    these videos are gold

  • @abhishekrai4325
    @abhishekrai4325 9 років тому +1

    brilliant !
    Very easy and efficient explanations.
    Thank you so much sir.

  • @tomasz-rozanski
    @tomasz-rozanski 8 років тому +23

    You can also implement print() function like this:
    void print(char *s)
    {
    while (*s)
    printf("%c", *s++);
    printf("
    ");
    }

    • @shivarajpatil162
      @shivarajpatil162 7 років тому +1

      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 роки тому +1

      @@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.

  • @menderelcapbig1876
    @menderelcapbig1876 4 роки тому

    perfect explanation

  • @carbonmc3782
    @carbonmc3782 6 місяців тому

    Wonderful. You’re so awesome.

  • @tarunkamineni92
    @tarunkamineni92 10 років тому +2

    thanks it helped me in passing character array as argument

  • @mahendraaanjna2005
    @mahendraaanjna2005 2 роки тому +1

    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 роки тому +1

      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.

  • @francisfraa
    @francisfraa 9 років тому +1

    Amazing Video which is mastered by your hard work
    Kudos!!!!

  • @rizwanmushtaq15
    @rizwanmushtaq15 6 років тому

    Really Awesome, great and very clear understanding of the concepts! Thank you very much!

  • @suyogchavan8779
    @suyogchavan8779 4 роки тому +2

    this may help me for passing this part

  • @mostafayasin1
    @mostafayasin1 6 років тому

    Many thanks

  • @akash7752
    @akash7752 6 років тому +1

    at 3:33 its backward slash

  • @dognip
    @dognip 3 роки тому

    This is gold.

  • @azizbekdavlatogli3371
    @azizbekdavlatogli3371 8 років тому +16

    9:24 why c2[1] is "l"? maybe it should be "e", as the second element of "hello"

  • @nikolajkapa2283
    @nikolajkapa2283 8 років тому

    Good quality and depth.

  • @rajupdl6
    @rajupdl6 7 років тому

    Awesome video for beginner like me ...

  • @jixuancheng
    @jixuancheng 5 років тому +1

    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.

  • @leixun
    @leixun 4 роки тому +2

    *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

  • @mizanurrahmankhan692
    @mizanurrahmankhan692 9 років тому +2

    very nice tutorial.
    thanks a lot.

  • @garhwaop
    @garhwaop Рік тому +1

    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

  • @staticinteger
    @staticinteger 10 років тому +2

    Amazing video!

  • @kevinspacey6090
    @kevinspacey6090 5 років тому +2

    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.

  • @SAGARKUMAR-ec6go
    @SAGARKUMAR-ec6go 6 років тому +2

    u r amazing sir!!

    • @sanambaloch2975
      @sanambaloch2975 5 років тому

      no he is not amazing......he made a lot of mistakes

    • @AjaySharma-le3df
      @AjaySharma-le3df 4 роки тому

      @@sanambaloch2975 like to share those mistakes?

  • @sunnyrauniyar6769
    @sunnyrauniyar6769 5 років тому +1

    thankyou so much, your videos are so convincing :-)

  • @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

  • @vishnuvardhan-dp4zc
    @vishnuvardhan-dp4zc 7 років тому

    And. We loved this explanation!👌

  • @perlecatt9123
    @perlecatt9123 11 місяців тому

    bro i love u for this thanks

  • @mahimajangra3248
    @mahimajangra3248 4 роки тому

    Thanks 😘

  • @mrpossible5696
    @mrpossible5696 5 років тому

    Thank you

  • @eur0dad
    @eur0dad 11 років тому +1

    your videos are amazing

  • @AnToxa57
    @AnToxa57 4 роки тому

    You really helped me out a lot!! Thank You!

  • @Varrian839
    @Varrian839 8 років тому

    Thank you very much! I finally find my bug after watching it.

  • @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

  • @muhammadshoaibfareed2577
    @muhammadshoaibfareed2577 6 років тому +1

    very good video .You explain it beautifully . Other series kb ay gi ???

  • @cengieyt
    @cengieyt 3 роки тому

    great video

  • @alexsece
    @alexsece 8 років тому

    Thank you!

  • @prosaa505
    @prosaa505 3 роки тому

    thank you sir

  • @govindrathi2940
    @govindrathi2940 6 років тому +3

    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?

  • @rakesh.kandula
    @rakesh.kandula 5 років тому

    Increementing of C works because. in print function it is reference value which can be incremented

  • @Shubham-ny2ce
    @Shubham-ny2ce 7 років тому

    Good to see such videos....

  • @asifrayan2420
    @asifrayan2420 7 років тому

    amazing videos sir.

  • @MrGentlemanism
    @MrGentlemanism 7 років тому +1

    C++ will work, because you are incrementing the start address that was given as a parameter!

  • @tapanjeetroy8266
    @tapanjeetroy8266 7 років тому

    thanks a lot sir...

  • @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)

  • @nathanbivens2394
    @nathanbivens2394 5 років тому +2

    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.

  • @totasalam7060
    @totasalam7060 10 років тому

    thank u very much you are simply amazing

  • @tingbinhuang4562
    @tingbinhuang4562 8 років тому

    amazing explaining, thank you :)

  • @hirokaanf4287
    @hirokaanf4287 10 років тому +4

    I believe that in a list the null terminated character is implicitly declared ?

  • @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.

  • @Harsh-yw4ct
    @Harsh-yw4ct 8 років тому

    Great Stuff!!

  • @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.

  • @youyiliu7556
    @youyiliu7556 6 років тому

    Amazing!

  • @MsMicro2007
    @MsMicro2007 10 років тому

    Thanks very useful for me

  • @ferranmolins5825
    @ferranmolins5825 6 років тому

    Muy buenas explicaciones, sin duda!

  • @god-speed03
    @god-speed03 5 років тому

    you are god to me

  • @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)

  • @mahyanaldosari2172
    @mahyanaldosari2172 11 років тому +2

    Life saver

  • @iumar28
    @iumar28 4 роки тому

    9:42 c2[1] will print 'e'

  • @fardinshaikh2816
    @fardinshaikh2816 2 роки тому

    at 9:26 , the output should be 'e'

  • @SJoshi-vn4ll
    @SJoshi-vn4ll 7 років тому

    please make more videos on programming

  • @raresraulea5455
    @raresraulea5455 5 років тому +1

    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!

  • @muckymcfly
    @muckymcfly 5 років тому +75

    Who's here in 2019?

  • @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.

  • @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