Character arrays and pointers - part 1

Sdílet
Vložit
  • čas přidán 8. 07. 2024
  • See complete series on pointers in C/C++ here:
    • Pointers in C/C++
    See part 2 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

Komentáře • 324

  • @keen2461
    @keen2461 Před 3 lety +55

    This guy explains really well. I have a full book on pointers, but I prefer to watch his videos. Awesome C course.

    • @palashbehra9303
      @palashbehra9303 Před 2 lety

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

    • @keen2461
      @keen2461 Před 2 lety +1

      @@palashbehra9303 The book is "Understanding pointers in C" by Yashavant Kanetkar. I bought it in India while on a business trip there.

    • @palashbehra9303
      @palashbehra9303 Před 2 lety +1

      @@keen2461 thanks man

    • @asterisk2598
      @asterisk2598 Před rokem

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

  • @thisaintmyrealname1
    @thisaintmyrealname1 Před 4 lety +18

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

  • @eriaardwen946
    @eriaardwen946 Před 8 lety +75

    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 Před 4 lety +3

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

    • @amansinghbhadauria2818
      @amansinghbhadauria2818 Před 4 lety

      @@akashsahu933 SHUT UP!

    • @sagivalia5041
      @sagivalia5041 Před 2 lety +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.

  • @harunwagura4727
    @harunwagura4727 Před 4 lety +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.

  • @GKT196
    @GKT196 Před 4 lety +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

  • @mycodeschool
    @mycodeschool  Před 10 lety +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.

  • @ceechi2408
    @ceechi2408 Před 8 lety +1

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

  • @chriswesley594
    @chriswesley594 Před 7 lety +25

    You have made a superb video - many thanks.

  • @bocanica1
    @bocanica1 Před 8 lety +2

    Thanks for your tutorial! You're the best!

  • @steveokinevo
    @steveokinevo Před 9 lety +5

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

  • @kaino1990
    @kaino1990 Před 5 lety +1

    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

  • @mohamedatef3526
    @mohamedatef3526 Před 5 lety +1

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

  • @alirezaghodsipoor2239
    @alirezaghodsipoor2239 Před 5 lety +1

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

  • @abhishekrai4325
    @abhishekrai4325 Před 8 lety +1

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

  • @carolinagordillo3307
    @carolinagordillo3307 Před 6 lety +2

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

  • @happyholi6328
    @happyholi6328 Před 6 lety +3

    Thank you very much admin you taught really classical

  • @ozzyfromspace
    @ozzyfromspace Před 5 lety +31

    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.

  • @sahareliyahu3462
    @sahareliyahu3462 Před 5 lety +1

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

  • @Fikri_A777
    @Fikri_A777 Před 4 lety +1

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

  • @rizwanmushtaq15
    @rizwanmushtaq15 Před 6 lety

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

  • @yashpandey9550
    @yashpandey9550 Před 8 lety +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 Před 8 lety +10

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

    • @saisrikar7987
      @saisrikar7987 Před 7 lety +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 Před 6 lety +5

      sai srikar lol😁 extra marks for steps 👍

    • @yanalashivaprasadreddy3010
      @yanalashivaprasadreddy3010 Před 5 lety

      it is a mistake.

    • @baderbboukheit3604
      @baderbboukheit3604 Před 4 lety +33

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

  • @DarkLevis
    @DarkLevis Před 7 lety

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

  • @tarunkamineni92
    @tarunkamineni92 Před 9 lety +2

    thanks it helped me in passing character array as argument

  • @staticinteger
    @staticinteger Před 9 lety +2

    Amazing video!

  • @francisfraa
    @francisfraa Před 9 lety +1

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

  • @simonribas4625
    @simonribas4625 Před 2 lety +1

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

  • @mizanurrahmankhan692
    @mizanurrahmankhan692 Před 8 lety +2

    very nice tutorial.
    thanks a lot.

  • @precisionchoker
    @precisionchoker Před 5 lety +1

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

  • @ferranmolins5825
    @ferranmolins5825 Před 5 lety

    Muy buenas explicaciones, sin duda!

  • @alyshawky9918
    @alyshawky9918 Před 3 lety

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

  • @yellowtinted
    @yellowtinted Před 2 lety

    thanks for your explaination!

  • @877dev3
    @877dev3 Před 4 lety

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

  • @sanchitchakraborty8146

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

  • @vinayvinay3031
    @vinayvinay3031 Před 2 lety

    Thank you for clearing my doubts

  • @eur0dad
    @eur0dad Před 10 lety +1

    your videos are amazing

  • @samdavepollard
    @samdavepollard Před 3 lety

    these videos are gold

  • @tingbinhuang4562
    @tingbinhuang4562 Před 7 lety

    amazing explaining, thank you :)

  • @SmartProgramming
    @SmartProgramming Před 6 lety +4

    awesome work sir, really appreciating, thank you 👍👍

  • @nikolajkapa2283
    @nikolajkapa2283 Před 7 lety

    Good quality and depth.

  • @alexsece
    @alexsece Před 7 lety

    Thank you!

  • @mostafayasin1
    @mostafayasin1 Před 5 lety

    Many thanks

  • @vishnuvardhan-dp4zc
    @vishnuvardhan-dp4zc Před 6 lety

    And. We loved this explanation!👌

  • @sunnyrauniyar6769
    @sunnyrauniyar6769 Před 5 lety +1

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

  • @totasalam7060
    @totasalam7060 Před 9 lety

    thank u very much you are simply amazing

  • @menderelcapbig1876
    @menderelcapbig1876 Před 3 lety

    perfect explanation

  • @sebyandrisan4750
    @sebyandrisan4750 Před 5 lety +3

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

  • @dognip
    @dognip Před 2 lety

    This is gold.

  • @Harsh-yw4ct
    @Harsh-yw4ct Před 8 lety

    Great Stuff!!

  • @ismoiljonabdumajidov9260

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

  • @tomasz-rozanski
    @tomasz-rozanski Před 7 lety +23

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

    • @shivarajpatil162
      @shivarajpatil162 Před 6 lety +1

      Please explain this print function.. as am not able to understand this.

    • @TheGBsXB
      @TheGBsXB Před 6 lety

      Or like this:
      void print(char *c){
      putchar(*c++);
      if(*c) print(c);
      }

    • @lordstark5292
      @lordstark5292 Před 4 lety

      @@shivarajpatil162 or like this void print(char *c)
      {
      int i = 0;
      while(c[i])
      {
      write(1, &c[i], 1);
      i++;
      }

    • @oliverbeck6839
      @oliverbeck6839 Před 4 lety

      wow very concise, well done

    • @cafarellano9340
      @cafarellano9340 Před 4 lety +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.

  • @youyiliu7556
    @youyiliu7556 Před 6 lety

    Amazing!

  • @hirokaanf4287
    @hirokaanf4287 Před 10 lety +4

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

  • @afafemad187
    @afafemad187 Před 11 měsíci

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

  • @asifrayan2420
    @asifrayan2420 Před 7 lety

    amazing videos sir.

  • @Varrian839
    @Varrian839 Před 8 lety

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

  • @tapanjeetroy8266
    @tapanjeetroy8266 Před 6 lety

    thanks a lot sir...

  • @mahyanaldosari2172
    @mahyanaldosari2172 Před 10 lety +2

    Life saver

  • @Shubham-ny2ce
    @Shubham-ny2ce Před 6 lety

    Good to see such videos....

  • @prashantdhawase8057
    @prashantdhawase8057 Před 3 lety

    Very beautiful handwriting ... Nice explanation too

  • @mrpossible5696
    @mrpossible5696 Před 5 lety

    Thank you

  • @rajupdl6
    @rajupdl6 Před 6 lety

    Awesome video for beginner like me ...

  • @suyogchavan8779
    @suyogchavan8779 Před 4 lety +2

    this may help me for passing this part

  • @cengieyt
    @cengieyt Před 2 lety

    great video

  • @nathamuni9435
    @nathamuni9435 Před 10 měsíci

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

  • @prosaa505
    @prosaa505 Před 3 lety

    thank you sir

  • @mahimajangra3248
    @mahimajangra3248 Před 4 lety

    Thanks 😘

  • @AnToxa57
    @AnToxa57 Před 3 lety

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

  • @sayantaniguha8519
    @sayantaniguha8519 Před 3 lety

    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

  • @perlecatt9123
    @perlecatt9123 Před 4 měsíci

    bro i love u for this thanks

  • @Skibadee99
    @Skibadee99 Před 7 lety

    nice video!

  • @anghelutadragos1898
    @anghelutadragos1898 Před 7 lety

    Hi,
    Could you suggest a list of exercices for pointers?

  • @deepTh00ught
    @deepTh00ught Před 3 lety

    thank you

  • @mahendraaanjna2005
    @mahendraaanjna2005 Před 2 lety +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 Před rokem +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 Před rokem

      @@mihai5085 you are correct

    • @Haris-bg4jy
      @Haris-bg4jy Před 11 měsíci

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

  • @variancaesar4778
    @variancaesar4778 Před 3 lety

    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)

  • @ritadeeproy996
    @ritadeeproy996 Před 9 lety +10

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

  • @rakeshkandula2318
    @rakeshkandula2318 Před 5 lety

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

  • @kimnjklmnjhh1851
    @kimnjklmnjhh1851 Před 6 lety

    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?

  • @MsMicro2007
    @MsMicro2007 Před 9 lety

    Thanks very useful for me

  • @aperson4051
    @aperson4051 Před 5 lety

    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?

  • @raresraulea5455
    @raresraulea5455 Před 4 lety +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!

  • @govindrathi2940
    @govindrathi2940 Před 5 lety +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 Před 3 lety

      same here i dont know why? have you found out yet?

  • @zarrafsharif3099
    @zarrafsharif3099 Před 6 lety

    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??

  • @SJoshi-vn4ll
    @SJoshi-vn4ll Před 6 lety

    please make more videos on programming

  • @Dk35840
    @Dk35840 Před 6 lety

    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

  • @muhammadshoaibfareed2577
    @muhammadshoaibfareed2577 Před 5 lety +1

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

  • @rootdefault6263
    @rootdefault6263 Před 4 lety

    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

  • @ozzmanr
    @ozzmanr Před 7 lety

    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?

  • @svelmurugan916
    @svelmurugan916 Před 8 lety

    what compiler are you used?

  • @SAGARKUMAR-ec6go
    @SAGARKUMAR-ec6go Před 5 lety +2

    u r amazing sir!!

  • @jixuancheng
    @jixuancheng Před 5 lety +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.

  • @nathanbivens2394
    @nathanbivens2394 Před 5 lety +1

    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.

  • @prasannadhage3878
    @prasannadhage3878 Před 6 lety

    Which compiler are u using?

  • @azizbekdavlatogli3371
    @azizbekdavlatogli3371 Před 7 lety +16

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

  • @devangsrivastava6736
    @devangsrivastava6736 Před 5 lety

    Sweet

  • @MPElectronique
    @MPElectronique Před 2 lety

    nice! :)

  • @shairazareen2922
    @shairazareen2922 Před 3 lety

    Lifesaver

  • @ramppuru4
    @ramppuru4 Před 8 lety

    sir which C compiler are you using

  • @aravindbabu2643
    @aravindbabu2643 Před 7 lety

    sir we can only icrement pointer variable only when it is assigned to other but how can we increment c.?

  • @naminhvan8632
    @naminhvan8632 Před 8 lety

    Dear author and all,
    Why the output of program without character '\0' is ok, didn't see same your result?
    I am using ubuntu 15.1 and g++ compiler.
    thank!

  • @bariswheel
    @bariswheel Před rokem

    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.