Character arrays and pointers - part 2

Sdílet
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

Komentáře • 158

  • @thestarinthesky_
    @thestarinthesky_ Před 4 lety +96

    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!

    • @shivamverma9447
      @shivamverma9447 Před 3 lety +4

      Exactly....Best explanation ever.......

    • @entropy3387
      @entropy3387 Před 3 lety +1

      He is dead.

    • @thestarinthesky_
      @thestarinthesky_ Před 3 lety +13

      @@entropy3387 his friend is dead. The one made the videos is alive and working in Google 🙌🏼😊

    • @entropy3387
      @entropy3387 Před 3 lety

      @@thestarinthesky_ Oh, my bad. Thanks for the heads up.

    • @amanpahwa211
      @amanpahwa211 Před 3 lety

      @Iker Donald this is a well known scam

  • @mohammedfawaz289
    @mohammedfawaz289 Před 7 lety +19

    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

  • @alastairh6460
    @alastairh6460 Před 9 lety +45

    Best description of memory allocation and the usefulness of pointers I've ever seen. Absolutely superb lecture series. Thank you.

  • @daevyd100
    @daevyd100 Před 8 lety +60

    These are the best computer science videos on the internet.

  • @naboulsikhalid7763
    @naboulsikhalid7763 Před rokem +2

    by far, The best ever explanation for Tricky POINTER. Thank you for making it easy to grasp.

  • @anthonynarvaez2582
    @anthonynarvaez2582 Před rokem +1

    I'm new to C programming, but the way you explain things makes so much sense. Thanks!

  • @user-ps5sj8si7k
    @user-ps5sj8si7k Před 5 lety +2

    You are absolutely genious! These are the most helpful videos about pointers I've seen... Thanks alot!!

  • @protopet9604
    @protopet9604 Před 7 lety +1

    Such an excellent job you did with all these videos! Thanks!

  • @steveokinevo
    @steveokinevo Před 10 lety +23

    Best videoz ever on pointers man seriously, helped alot...Thank you...Thank you.

  • @sanchitchakraborty8146
    @sanchitchakraborty8146 Před 6 lety +9

    I loved that part sir
    incrementing a pointer increments the address by the size of that data type.. awesome

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

    Thank you sir, I've understood the concepts of pointers after a long time!

  • @honeychopra2560
    @honeychopra2560 Před 6 lety +8

    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.

    • @gamoholic7653
      @gamoholic7653 Před 3 lety +8

      The truth is, only a minority part of Internet users are here to learn something, and the majority are just wandering here for entertainment.

  • @1NinjaSamurai
    @1NinjaSamurai Před 6 lety

    Thank You So Much. Everything in my book and lectures makes so much more sense now.

  • @AbhinavPathak7
    @AbhinavPathak7 Před 9 lety +62

    Every other teacher irritates me, but not you. Thank you

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

    thanks, sir, for the concept of string constant and constant pointer(array).

  • @arpitagarwal159
    @arpitagarwal159 Před 7 lety

    Superb video. Hats off to your dedication. This is the best video series for memory concepts.

  • @mouadzehari1724
    @mouadzehari1724 Před 7 lety +1

    pointers are wonderful you need just a person like you to explain it (Y)

  • @leixun
    @leixun Před 4 lety +19

    *My takeaways:*
    1. char* vs char[] 7:30

  • @yosefhayle2605
    @yosefhayle2605 Před 7 měsíci

    Great man, I like ur simplicity and clarity. Genius way of explanation.

  • @lamebrisee
    @lamebrisee Před 4 lety

    the best demistifying pointers tutorial ever !!

  • @burakalpyasar755
    @burakalpyasar755 Před 2 lety

    Bro I love you so much, I did my OpSys homework, thanks to you

  • @allaryyan6608
    @allaryyan6608 Před 6 lety

    wow, what an amazing explaination, thank you so much!!

  • @tausal1
    @tausal1 Před 6 lety

    Excellent explanation. Thank you so much!

  • @abhisek31
    @abhisek31 Před 7 lety +1

    really helpful. He explained it really well and easily.

  • @kiku_to_herself
    @kiku_to_herself Před 3 lety

    U are amazing .
    Thank you so much for this playlist ❤️

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

    Awesome explanation sir,you are the best ,make more video like that.thank you sir

  • @Brono25
    @Brono25 Před 5 lety

    i love your editing. good pace.

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

    great explanation, very helpful tutorial, thanks a lot sir 👍👍

  • @mycodeschool
    @mycodeschool  Před 11 lety

    its working fine for me. I will have to see your code to help.

  • @vinayppandey4321
    @vinayppandey4321 Před 3 lety

    Bestt pointer tutorials yr....😊😊

  • @susmitroy2398
    @susmitroy2398 Před 6 lety

    This is the best..............Thanks Sir....

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

    Excellent video

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

    best video ever ... keep on going
    your fan from iraq

  • @rohitbhardwaj8775
    @rohitbhardwaj8775 Před 3 lety +1

    They are really helping me building, although I am in high school 👌

  • @MehulBhujwala
    @MehulBhujwala Před 10 lety +5

    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.

    • @varunsubramanian5484
      @varunsubramanian5484 Před 5 lety

      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.

  • @dharmikmistry8781
    @dharmikmistry8781 Před 4 lety

    Best channel 👏👏🤗

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

    Lovely.

  • @crummmycheese
    @crummmycheese Před 4 lety

    Thank you

  • @ericchu6503
    @ericchu6503 Před 4 lety

    super helpful!!!!

  • @anondoggo
    @anondoggo Před 4 lety

    Thank you for saving my degree.

  • @asifmahmud6692
    @asifmahmud6692 Před 5 lety

    its just awesome ...
    tnx ...

  • @nosiphiweduma3126
    @nosiphiweduma3126 Před 9 lety +20

    you are so awesome , where have you been all my coding life :) :D

    • @mohammedfawaz289
      @mohammedfawaz289 Před 7 lety

      ikr? same question !

    • @piyushpahwa7897
      @piyushpahwa7897 Před 6 lety +8

      Can't call it a 'coding life', if you don't understand pointers.

    • @dansintean6820
      @dansintean6820 Před 6 lety +9

      You actually can. With languages that don't use pointers.
      And yes, there is a point to not use pointers.

    • @nicolareiman9687
      @nicolareiman9687 Před 5 lety +5

      @@dansintean6820 Only Pussies who can't understand pointers and the languages that you talk about are using pointer in the back-end.

  • @mycodeschool
    @mycodeschool  Před 11 lety

    It should work. Its working for me. You are passing s to printf, right?

  • @hfe1833
    @hfe1833 Před 3 lety

    I like this tutorial almost bare metal programming

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

    You are awesome 😍

  • @subhasishmondal5211
    @subhasishmondal5211 Před 3 lety

    great buddy

  • @sunritroykarmakar4406
    @sunritroykarmakar4406 Před 2 lety

    brilliant

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

    super tutorial

  • @ekhliousful
    @ekhliousful Před 7 lety

    @8:28 how the string will be stored as constant in the text segment?

  • @aarenmathew6258
    @aarenmathew6258 Před 2 lety +2

    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.

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

    You are love

  • @sixtusonyedibe1619
    @sixtusonyedibe1619 Před rokem

    Did you just say thanks for watching??? When we should be the one thanking you. Awesome videos

  • @roylee3196
    @roylee3196 Před 9 lety +13

  • @dognip
    @dognip Před 2 lety

    So I wrote this code:
    int main()
    {
    char c[5];
    cin>>c;
    cout

  • @martianpirate9657
    @martianpirate9657 Před 3 lety

    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.

  • @mycodeschool
    @mycodeschool  Před 11 lety +6

    So, it must be some error in your function.

  • @nguyenkhanh7759
    @nguyenkhanh7759 Před měsícem

    thank you, you will be missed

  • @anonym_3241
    @anonym_3241 Před 9 měsíci +1

    2023 really helped, usually i am not commenting type, even tho this video is old yet still gold

  • @letsbeproductive7675
    @letsbeproductive7675 Před 7 lety +2

    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

  • @lamchannekwafa8399
    @lamchannekwafa8399 Před 8 lety

    in c++ .. I have a problem to have the adresse of elements of array char ... pliz help m

  • @sanjusingh1896
    @sanjusingh1896 Před 4 lety

    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

  • @hassenbensaad8862
    @hassenbensaad8862 Před rokem +1

    Feb. 2023 - Best pointers explantation.

  • @aman-gy5nn
    @aman-gy5nn Před 3 lety

    what is ide you are using?

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

    please upload videos on STL.....

  • @sapthamaaleekaas6228
    @sapthamaaleekaas6228 Před 7 lety +2

    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';

    • @iashishhere
      @iashishhere Před 6 lety

      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

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

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

  • @clar331
    @clar331 Před 6 lety

    someone please explain "const", what does it do?

  • @shagun2228
    @shagun2228 Před 6 lety

    char pointer would take the memory of one byte or 4 bytes in the stack frame ?
    3:51

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

      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.

  • @hocketstar
    @hocketstar Před 8 lety +4

    better than my uni lecturer :)

    • @theamjolnir9641
      @theamjolnir9641 Před 6 lety

      Same. I understand a lot more from this channel's videos than my school.

  • @neddyned3802
    @neddyned3802 Před 7 lety

    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.

    • @Manisood001
      @Manisood001 Před 6 lety

      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

  • @theamjolnir9641
    @theamjolnir9641 Před 6 lety +7

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

    • @sandrok14
      @sandrok14 Před 5 lety +5

      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

  • @prynzeshanemangco4165
    @prynzeshanemangco4165 Před 3 lety +1

    I love how the video is exactly 10 mins

  • @anmoljain1012
    @anmoljain1012 Před 3 lety

    you are GOD

  • @kemosabe6521
    @kemosabe6521 Před 6 lety

    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

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

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

  • @riyadunnabi
    @riyadunnabi Před 5 měsíci

    best best

  • @manavjain5923
    @manavjain5923 Před 7 lety +1

    why does char * c stores string as a constant while char c[20] doesn't??

    • @theamjolnir9641
      @theamjolnir9641 Před 6 lety +5

      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";

    • @riteshbhatt9476
      @riteshbhatt9476 Před 3 lety

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

  • @kirankumar3003
    @kirankumar3003 Před 11 lety +1

    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

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

      Make sure the printf() function has the correct placeholder. %c for char and %s for string.

  • @BeastGaming-hq6yu
    @BeastGaming-hq6yu Před 4 lety

    the C++ will do
    because it will increase the address an print element on that address

  • @abdelrhmanahmed1378
    @abdelrhmanahmed1378 Před 3 lety +1

    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

  • @dukjongwon9011
    @dukjongwon9011 Před 7 lety +1

    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.

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

      All these downwards and up words are just an human assumption.

  • @pranavshah8484
    @pranavshah8484 Před 4 lety

    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"

  • @vaibhavgupta5636
    @vaibhavgupta5636 Před 6 lety

    showing error on changing to char*c
    warning: deprecated conversion from string constant to 'char*'
    why?

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

    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

    • @mycodeschool
      @mycodeschool  Před 10 lety +25

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

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

      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?

    • @Phantom1037
      @Phantom1037 Před 7 lety +1

      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.

  • @alberteinstein4123
    @alberteinstein4123 Před 4 lety

    how a character pointer variable can store a 4 byte integer address data?

    • @PaAGadirajuSanjayVarma
      @PaAGadirajuSanjayVarma Před 4 lety

      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

    • @PaAGadirajuSanjayVarma
      @PaAGadirajuSanjayVarma Před 4 lety

      it is storing address not value REMEMBER

  • @leontube007
    @leontube007 Před 3 lety

    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.

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

    do pointers exist in other programming languages?

    • @mohammedfawaz289
      @mohammedfawaz289 Před 7 lety

      sure...

    • @theamjolnir9641
      @theamjolnir9641 Před 6 lety

      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.

    • @abhishektanwar8576
      @abhishektanwar8576 Před 6 lety

      Most of the other languages like Java have concept of "References" which are less confusing than concept of "Pointers" in C/C++.

  • @vishaljhaveri7565
    @vishaljhaveri7565 Před 4 lety

    8:20

  • @yourdailymusic6906
    @yourdailymusic6906 Před 8 lety

    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?

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

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

    • @sachinmadishetty7978
      @sachinmadishetty7978 Před 8 lety

      in char *s=//u can assign only single character

  • @manupareek8776
    @manupareek8776 Před 4 lety

    Rest in peace.........

  • @kirankumar3003
    @kirankumar3003 Před 11 lety

    no...to user defined function print

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

    how char pointer store 4 bytes in memory ??? in- spite of it char store 1 byte only ???

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

      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.

    • @ashish3944
      @ashish3944 Před 4 lety

      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.

  • @AshutoshKumar-mv5um
    @AshutoshKumar-mv5um Před 6 lety

    But in last video u changes c[0] to A and printed AELLO. What was that>??

    • @nikoladjosan9502
      @nikoladjosan9502 Před 6 lety

      When he scaned HELLO
      H got index 0,when he put c[0]='A' it changed to AELLO that is simple mate :)

  • @BiaCh-qu6ji
    @BiaCh-qu6ji Před 6 lety

    Sir, how the pointer takes 4 bytes of memory, while it is of char type ?

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

      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.

    • @kuldeepkumarpandey3881
      @kuldeepkumarpandey3881 Před 6 lety

      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.

    • @FoxSensei
      @FoxSensei Před 5 lety

      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

  • @shivamverma9447
    @shivamverma9447 Před 3 lety +1

    Sacred Lectures..

  • @ayushimishra3032
    @ayushimishra3032 Před 4 lety

    Why is character pointer stored in 4 bytes.It has too be stored in 1 byte then why is it so ??

  • @probhakarsarkar2430
    @probhakarsarkar2430 Před 7 lety +1

    2:46 it should be 100 -119

  • @jatinchavan5360
    @jatinchavan5360 Před 8 lety

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

  • @kratossh8011
    @kratossh8011 Před 7 lety

    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);
    }

    • @manugomez7319
      @manugomez7319 Před 7 lety

      see the second last line printf(c) . Change it to print(c). Cheers.

    • @clar331
      @clar331 Před 6 lety

      in your main you have "printf" it should be "print" method

  • @pawankumawat2667
    @pawankumawat2667 Před 7 lety

    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);
    }

    • @pawankumawat2667
      @pawankumawat2667 Před 7 lety

      why is it giving same o/p on using foo(p) and foo(p++)

    • @markogavrilovic749
      @markogavrilovic749 Před 7 lety

      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.

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

      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.

    • @theamjolnir9641
      @theamjolnir9641 Před 6 lety

      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!

  • @Aditya-mg9se
    @Aditya-mg9se Před 3 lety

    time 3 :21