Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

A.Avinash_Goutham

main() { char *s[] = { "apple","mango","berry","cherry"}; printf("%c\n",*(s+2)); printf("%c\n",( *s+2)); } i tried this program and i got some un xpected results.... u ppl can find an xplanation for this?

  • one year ago
  • one year ago

  • This Question is Closed
  1. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 0

    well 1 not too sure what the * is doing there, also when dealing with an array you want to use the s[] not s... FOr example s[0] is apple, s[1] is mango etc.....

    • one year ago
  2. rsmith6559
    Best Response
    You've already chosen the best response.
    Medals 0

    The first printf looks to dereference s+2, the second looks like it dereferences s and adds 2 to it.

    • one year ago
  3. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    @rsmith6559 sir please try compiling it ....it was not that easy

    • one year ago
  4. slotema
    Best Response
    You've already chosen the best response.
    Medals 0

    Since you have defined s as a pointer to a character array (char **), dereferencing s will give you the pointer to a string (like e.g. 0x400624). Since you said you want to print this value as a character (since you use '%c' in printf), these addresses are somehow translated into characters. I don't know how that translation is done, but it could result in printing some weird characters on your screen.

    • one year ago
  5. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    no i dont think so....coz we all get the same result... i mean we all got a star....so it cant be a random adrees in RAM

    • one year ago
  6. slotema
    Best Response
    You've already chosen the best response.
    Medals 0

    I did not get a star, but a 0 and an &. During compilation, the compiler will put those constant strings somewhere in the executable (if you're on Linux, try string <executable> to find such strings). Those addresses are therefor fixed once you compiled the code, so you will see the same symbol each time you run that executable.

    • one year ago
  7. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    ohh ok wait lemme run on linux an @slotema plz do wait i have another query

    • one year ago
  8. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    ok fine my next one

    • one year ago
  9. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    #include<iostream> int main() { int x[3][5]={ {18,20,13,24,35}, {7,8,6,9,10},{19,22,30,21,15}} ; int *n=&x[0][0]; std::cout<<"endl:- "<<(*(n+3)+1) <<":"<<*(*x+2)+5<<"\n"; std::cout<<"endl:- "<<*(*(x+2)+1) <<":"<<*(x[1]+2)+5; } u have amy idea how that *(n+3)+1 works?

    • one year ago
  10. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    *any

    • one year ago
  11. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    int main() { int x[3][5]={ {18,20,13,24,35}, {7,8,6,9,10},{19,22,30,21,15}} ; int *n=&x[0][0]; std::cout<<"endl:- "<<(*(n+3)+1) <<":"<<*(*x+2)+5<<"\n"; std::cout<<"endl:- "<<*(*(x+2)+1) <<":"<<*(x[1]+2)+5; }

    • one year ago
  12. slotema
    Best Response
    You've already chosen the best response.
    Medals 0

    So n points to the first element from x (which is 18). *(n+3) take the fourth (n, n+1, n+2, n+3) element in the array (which is 24). That 24 is than incremented by 1.

    • one year ago
  13. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    i thot it's a 2-d array so incrementin n by 3 shud make it point to the 3-row instead?

    • one year ago
  14. slotema
    Best Response
    You've already chosen the best response.
    Medals 0

    if you want it to act as a 2-D array, you should change the definition of n. Right now, n is just an int *, so it acts as a 1-D array.

    • one year ago
  15. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    so if i make it like int ** it's gonna change it's behavior?

    • one year ago
  16. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    so what if n is an int ** i got smthn like 12

    • one year ago
  17. slotema
    Best Response
    You've already chosen the best response.
    Medals 0

    It a bit more complicated than that. If you want to use a pointer as a 2-D array, the compiler should know the size of the second dimension. Only if that size is known, the compiler will know what do with a statement like n+1. Therefor, you should define n as `int (*n)[5]` (post #6 in http://devmaster.net/forums/topic/12966-how-to-create-a-pointer-to-2d-array-in-c/page__view__findpost__p__69890).

    • one year ago
  18. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    it's about making an array of pointers r8? i was trying to work on pointer arithmetic

    • one year ago
  19. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    what's if i declare a double pointer say t int **t=&x[0][0]; and now i d like to use (*(t+3)+1) i expected a segmentation fault but i dint get it *(*(t+3)+1) gives me a segmentation fault....i convinced my self saying that ther's no x[3][1]; am i right?

    • one year ago
  20. slotema
    Best Response
    You've already chosen the best response.
    Medals 0

    There is indeed no x[3][*]. Try with 0, 1 or 2. I can't get the int ** pointer to compile properly without any errors. The solution in my previous post works for me.

    • one year ago
  21. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    did u try this (*(t+3)+1) i got a warning but i got a value 39

    • one year ago
  22. slotema
    Best Response
    You've already chosen the best response.
    Medals 0

    My problem is that my compiler (GCC-4.7) won't accept `int **t = &x[0][0];` But as long as it works for you, that's the most important thing.

    • one year ago
  23. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    it wont? :-/ but i want an xplanation :(

    • one year ago
  24. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    i got a 4.6.3

    • one year ago
  25. slotema
    Best Response
    You've already chosen the best response.
    Medals 0

    Did you already try changing the 3 into 2 for `*(*(t+3)+1)`?

    • one year ago
  26. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    no a min

    • one year ago
  27. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    ops sory i got a 39 for (*(t+2)+1) abd 12 for (*(t+3)+1)

    • one year ago
  28. slotema
    Best Response
    You've already chosen the best response.
    Medals 0

    The thing with `(*(t+2)+1)` is that you're derefencing t once. Whatever you print is not the value of the array, but again a pointer to the array. Try dereferencing the whole thing.

    • one year ago
  29. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    that gives a segmentation fault

    • one year ago
  30. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    may be it's linearly accesing the array on memory now taking sizeof(t) and traversing over?

    • one year ago
  31. slotema
    Best Response
    You've already chosen the best response.
    Medals 0

    My guess is that it's somehow due to the fact that the compiler does not know what to do with a t+1. How many bytes would it skip? Try the approach I mentioned earlier (int (*t)[5]).

    • one year ago
  32. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    i kno that would work it's simply creating an array of pointer and storing the address of beginning of row in that array i jus wanna jus more on pointer arithmetic it kinda confuses me everytime

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.