Page 1 of 2 1 2 LastLast
Results 1 to 20 of 35

Thread: Data Structures     submit to reddit submit to twitter

  1. #1
    Corwens a slot
    Join Date
    Apr 2006
    Posts
    4,115
    BG Level
    7

    Data Structures

    So I'm in this class, and I can't seem to find anyone that can explain this. I've taken C, I believe I have a pretty good grasp on pointers, but on my review for data structures it has these 2 "programs" and asks what each output is. Let me know if you can explain it to me as I seem to be at a loss for what they are doing. This is for a test review, not for any homework.

    Code:
    #include <stdio.h>
    int main (void)
    {
    	int a[5] = {5,4,3,2,1};
    	int i = 2;
    	int *p = &a[2];
    
    	printf("%d %d", *(a+i), p[3]);
    	system("pause");
    }
    and

    Code:
    #include <stdio.h>
    int main (void)
    {
    	int a[5] = {5,4,3,2,1};
    
    	printf("%d %d", *a, a[4]);
    
    	system("pause");
    }
    Thanks for any input, they both do compile, however I'm pretty sure the first one is drawing some random memory address as its like a 6 digit number.

  2. #2
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    Is the problem that the output isn't as you originally expected after running the program?

    If you're just looking for output, just run 'em through the compiler and observe. Your conclusion on the first program, as I'm understanding it, should be very very wrong.

  3. #3
    Bagel
    Join Date
    Oct 2006
    Posts
    1,226
    BG Level
    6

    pointers are gay

    but yeah for the first one I think your just looking at garbage values.

    The second one I think the first number is a garbage value(or maybe return 5) and the second one should return a 1.

  4. #4
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    Quote Originally Posted by Stee View Post

    but yeah for the first one I think your just looking at garbage values.
    no

    more refined answer:

    First program -- Not even close

    for second program: it's not a 'garbage' value, it has a very important significance. the a[4] return value is correct.

  5. #5
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    refining question for OP, just in case I wasn't clear: Did you write/compile the programs and get output from them? If so, what was your output and why do you think you got them?

    If you're not sure of the latter, ask that and then I'll tell ya

  6. #6
    Bagel
    Join Date
    Oct 2006
    Posts
    1,226
    BG Level
    6

    Second ones result is 5, 1

    *a being a[0] which is 5 and a[4] being 1


    just ran it through a compiler

  7. #7
    Corwens a slot
    Join Date
    Apr 2006
    Posts
    4,115
    BG Level
    7

    I got them printed on a review sheet, typed them identically, except that I actually semi-formatted them. I compiled them and these were the outputs they gave, I have no understanding of why they gave these outputs. I do know how pointers work and I do understand the a[4].

    First one gave: 3 1638192

    Second one gave: 5 1

  8. #8
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    making a good informative post in a previous edit, gimme a min!

    and yea you typed something wrong for the first one then

  9. #9
    Corwens a slot
    Join Date
    Apr 2006
    Posts
    4,115
    BG Level
    7

    Quote Originally Posted by Zhais View Post
    making a good informative post in a previous edit, gimme a min!

    and yea you typed something wrong for the first one then
    apparently double posted

  10. #10
    Corwens a slot
    Join Date
    Apr 2006
    Posts
    4,115
    BG Level
    7

    Quote Originally Posted by Zhais View Post
    making a good informative post in a previous edit, gimme a min!

    and yea you typed something wrong for the first one then
    I didn't type anything wrong lol
    Code:
    #include <stdio.h>
    int main (void)
    {
    int a[5] = {5,4,3,2,1};
    int i = 2;
    int *p = &a[2];
    printf("%d %d", *(a+i), p[3]);
    
    return 0;
    }
    this is exactly what is typed

  11. #11
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    Yahahaha, this is what I love about programming. With arrays, when you walk off the ass end of an array, you get different values depending on what was previously allocated there.... Still typing out the post, but I just misinterpreted what was going on before. I feel the program should use p[0], p[1], or p[2] to illustrate the concept better....

    Still typing >:D

  12. #12
    New Merits
    Join Date
    Feb 2006
    Posts
    241
    BG Level
    4

    I don't usually do this but here you go. My assumption is that you're doing a CS major (or else why go as far as data structures?) and if that's the case then you better learn this if you expect to get a job as a programmer rather than an IT phone guy.

    Quote Originally Posted by Minions View Post
    Code:
    #include <stdio.h>
    int main (void)
    {
    	int a[5] = {5,4,3,2,1};
    	int i = 2;
    	int *p = &a[2];
    
    	printf("%d %d", *(a+i), p[3]);
    	system("pause");
    }
    First: Obviously the variable a is an array. In the C language, arrays are pointers, just with simpler access syntax. So when you say a[x] you're actually saying *(a+(x*size)), or the value stored at the address found when you start with a (which, again, is a pointer, so it's an address) and add some number of offsets (x) of whatever size is being stored (for example, in C your standard in will be 4 bytes). So for example, if you declare an array of ints, and the address ends up being at say 0x000044, then value held by a[0] is at that base address, the value held at a[1] is at the base address + 4 bytes or 0x000048, etc.

    Next: What is p? p is a pointer which is set to &a[2], which you can write more clearly as p = &(a[2]), or p is set to the address of the 3rd item held by the a array. So if you were to dereference p, you'd get the value at a[2], or 3.

    So: The output is 2 decimal numbers right next to each other. What makes this wierd is that the value you are addressing in the first one is not right. What you are trying to print out is *(a + i), or *(a + 2), or the Value stored at the address pointed to by (a + 2). Now remember that bytes are typically 4 bytes in size; when you take a pointer and increment it by a smaller number than the size of the piece of data being held at that location, it screws things up. In that case, I'm really not sure what is supposed to happen-this is generally known as a *bug*. Think of it this way. You have 8 sequential bytes of memory. The first 4 store a 4 byte integer. The second 4 store a 4 byte integer. What if you try to look at a number starting in the middle of the first and ending in the middle of the second, which is what I suspect would happen? Well, it depends on the numbers being stored (i.e. if both are stored 0 you'll just get 0, but this is not normal). What you'll get is whatever the 4-byte representation is of the 2nd half of the first number concatenated with the 1st half of the second number.

    As for [p3], that's another bug. if p points at a[2], then p[0] = a[2], p[1] = a[3], p[2] = a[4], and thus p[3] = a[5], which is undefined since a is only a 5 element array. Now what I mean when I say it's undefined is that there will be something in memory when you access it-it's just generally impossible to know what (in certain conditions you can predict it but given this scenario there's nothing to suggest you can).



    Quote Originally Posted by Minions View Post
    Code:
    #include <stdio.h>
    int main (void)
    {
    	int a[5] = {5,4,3,2,1};
    
    	printf("%d %d", *a, a[4]);
    
    	system("pause");
    }
    Thanks for any input, they both do compile, however I'm pretty sure the first one is drawing some random memory address as its like a 6 digit number.
    This one is easy. Again, a is a pointer. Therefore *a is dereferencing the pointer, which takes you to the first element of the array. Therefore *a will print 5. a[4] is trivial, it's the 5 element of the array, or 1. So you should get 51.


    TL;DR:
    1st program: First value is a mishmash between 2 values and is generally considered a bug. Second value is off the end of the array so it's a garbage value.
    2nd program: You'll get 5 1.

  13. #13
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    This is what I typed!

    First program, values you should get in return are 3 and 2 respectively.

    Code:
    int *p = &a[2];
    To understand this statement, remember/know that an array is (usually) just a contiguous block of allocated memory. It's size is dependent on the the storage type (int, double, char, etc.) and how many elements it has. In this case, the size of the array is equivalent to

    Code:
     sizeof(int) * 5
    An array accessed by index will give you the value at it's base address offset by the index value. So an array at base address 0x000000 will have appropriate values at 0x0 + sizeof( int ) * index.

    The ampersand is designated to return the memory address of the object immediately after it. If you print "&a" you will have the base address of the array.

    Code:
     &a[2]
    ^ This piece of code will give you the address of the array at offset 2. If you are using 32-bit integers, this memory address will be the base address + 64. This is stored in the pointer value for the next use.

    So now we have the variable p. This essentially becomes a new array of length 3 starting at the address of a[2]. So when you print out p[0], p[1], p[2] you will get the values of {3,2,1}. p[3] is not part of the previously allocated array, so there is no guarantee of what value you will get. I initially got a value of '2', which kind of caused me to ignore some of the code and presume something else. This value could be something previously initialized in code, or an actual 'garbage' value that doesn't mean anything.

    In the printf statement,

    Code:
     *(a+i)
    Will give you the value of a[2] essentially. It takes the base address of a, adds the integer size * i, and gives you the value at that address. Again, I feel there are better ways to illustrate this concept more simply. I dislike your data structures teacher right now.


    edit: My explanation is inherently better because I say so!

  14. #14
    Corwens a slot
    Join Date
    Apr 2006
    Posts
    4,115
    BG Level
    7

    Zosi explained it too me, the first one, and I understand it now, and its actually an error on the teachers part for the second number.

    The first one once someone explained *a = a[0] I got it, thanks for the help.

  15. #15
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    and yea, the p[3] value could be considered a bug. Depends on what the purpose of that code snippet is (Are you identifying the bug, or just identifying the concept). If the latter, p[0] thru p[2] would demonstrate this much better. If the former, 'tis all good.

    I'd disagree that *(a+i) is a mismash of values. Try changing the values, you'll output that third value every time. It works because the offset inside the parenthesis is the same data type as what is in the array. If you had a char array, (a+'a') would give you a correct value as well I do believe...

  16. #16
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    Quote Originally Posted by Minions View Post
    its actually an error on the teachers part for the second number.
    Ya ha!

  17. #17
    New Merits
    Join Date
    Feb 2006
    Posts
    241
    BG Level
    4

    Quote Originally Posted by Zhais View Post

    In the printf statement,

    Code:
     *(a+i)
    Will give you the value of a[2] essentially. It takes the base address of a, adds the integer size * i, and gives you the value at that address. Again, I feel there are better ways to illustrate this concept more simply. I dislike your data structures teacher right now.


    edit: My explanation is inherently better because I say so!
    Actually I disagree, assuming the size of ints being used here is actually 4 bytes. If you declare a pointer in C and then use it as an address, I'm pretty sure that you need to increment it by the size of the integer or you get the mishmash I was talking about. so if the integers are at 0x0000 and 0x0004 and a points at 0x0000 then when you say *(a + 2) you actually try to dereference (0x0002), resulting in some horrible situation that makes good CS grads pull their hair out. I could be wrong, but I'm pretty sure.

  18. #18
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    Quote Originally Posted by shieldsofwindurst View Post
    Actually I disagree, assuming the size of ints being used here is actually 4 bytes. If you declare a pointer in C and then use it as an address, I'm pretty sure that you need to increment it by the size of the integer or you get the mishmash I was talking about. so if the integers are at 0x0000 and 0x0004 and a points at 0x0000 then when you say *(a + 2) you actually try to dereference (0x0002), resulting in some horrible situation that makes good CS grads pull their hair out. I could be wrong, but I'm pretty sure.
    Changing the value of i, or changing values in the array still get the expected result as though it was just a[i]. Been messing around with it to be sure, and I'm getting returns of {5,4,3,2,1} for i = {0,1,2,3,4} respectively.

  19. #19
    New Merits
    Join Date
    Feb 2006
    Posts
    241
    BG Level
    4

    Quote Originally Posted by Zhais View Post
    Changing the value of i, or changing values in the array still get the expected result as though it was just a[i].
    Maybe I just work with some really oldschool compilers then. Carry on, no pointer havoc here.

  20. #20
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    but pointer havoc is so fun!

    I remember this stuff fairly well, because I thought it was so damned nifty when I learned it. Let me figure out how iterators worked before I knew what iterators were.



    Sorry again minions for saying you typed it out wrong. I glazed over the error with walking off the array and assumed incorrectly that the output was right on my screen since it didn't give me a crazy value.

Page 1 of 2 1 2 LastLast

Similar Threads

  1. Cached data
    By Not Kuno in forum General Discussion
    Replies: 7
    Last Post: 2006-10-25, 18:49
  2. Jilted woman deletes ex's online RPG data
    By Sadler in forum General Discussion
    Replies: 8
    Last Post: 2005-01-31, 02:53