Thread: Studying for Final, Couple Questions...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    18

    Studying for Final, Couple Questions...

    I am studying for my final, which is tomorrow. It is an intro to C class and I have just a couple questions. He gave us this sample final and I don't get a couple of them and was seeing if someone could help me understand what is happening. Here goes...



    Question #1: What is the output of the following code segment, assuming strcopy() works without problem?

    Code:
    int main()
    {
           char data[10], *p;
    
           strcpy(date, "hello");
    
           p = data;
    
           while (*p != '\0')
                      *p++ += 1;
    
           printf("%s", data);
    
           return 0;
    }
    The answer is "ifmmp" so I guess I don't understand what is hapenning in the while section, if anyone could explain that it would be great.



    Question #2:
    What is the output of the following code segment?

    Code:
    int foo[3][3];
    
    int main(_
    {
    	
    	int i, j;
    	int *p;
    
    	for (i=0; i<3; i++)
    		for (j=0; j<3; j++)
    			foo[i][j] = (i+1) * (j+1);
    
    	p = foo;
    
    	printf("%d", *(p+7));
    
    	return 0;
    }
    I guess I don't get what is happening at the printf statement with
    *(p+7)...what does that do?

    The answer according to him is 6.



    Question #3:
    What is the output of the following code segment?

    Code:
    struct list{
    		int data;
    		struct list * next;
    		};
    int main()
    {
    		struct list a, b, c;
    		
    		a.data = 3;
    		b.data = 1;
    		c.data = 2;
    
    		a.next = &b;
    		b.next = &c;
    
    		printf("%d\n", a.next->next->data);
    
    		return 0;
    }
    His answer is 2, but I have no clue how he got it. I don't really get the printf statement, so if anyone does, please help.

    Thanks in advance for anyone who helps me, I really appreciate it and so does my grade.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > The answer is "ifmmp" so I guess I don't understand what is hapenning in the while section
    'h' + 1 == 'i'

    > *(p+7)...what does that do?
    Would writing it as p[7] help?

    > His answer is 2, but I have no clue how he got it.
    Well there are only 3 numbers to choose from

    Given a.next-> as being the same as b, can you figure out where another next-> takes you?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    in #1 the while loop is taking the characters "hello" and simply adding one to each of the letters

    eg

    h + 1 = i
    e + 1 = f
    l + 1 = m
    l + 1 = m
    o + 1 = p

    remember the characters are numerically listed (go look for an ascii chart and look up each letter and find its corresponding number). Try modifying the printf statement to %d to print the corresponding number of the letters, during the loop both before and after the +1, to see what i mean.

    in #2 this is a simple matrix structure (2d array)

    the 2d array is stored in memory as a 1d dimensional array, so when you are calling the statement:

    Code:
     *(p+7)
    you are accessing the 7th element in the array, which corresponds to the value 6. Try printing out the value of each element as they are inserted into the matrix to understand what values are put in here.

    in #3 you have a very simple linked list structure

    the statements

    Code:
    a.data = 3;
    This is setting the data field of the first element of the list to be 3. etc

    the next field in the struct is a pointer to the next element in the list. so when you call

    Code:
    printf(("%d\n", a->data);    --> 3
    printf(("%d\n", a->next->data);    -> 1
    printf(("%d\n", a->next->next->data);   -> 2
    The -> operator accesses the list's field, so when you access next your are actually accessing the next element in the list. so next->next->data actually is referring to the third elements data field. Hence the answer is 2.

    I hope this helps out.
    Last edited by ventolin; 05-10-2004 at 09:59 AM.

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    printf(("%d\n", a->data); --> 3
    not so, a is not a pointer:
    Code:
    printf("%d\n", a.data); /* 3 */ 
    printf("%d\n", a.next->data);     /* 1 */
    printf("%d\n", a.next->next->data);   /* 2 */
    Last edited by viaxd; 05-10-2004 at 02:12 PM.

  5. #5
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    thanks for "pointing" that out

    sloppy cut and pasting is to blame and i was in a rush whilst typing that
    Last edited by ventolin; 05-10-2004 at 05:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  2. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 01:04 AM
  3. A Couple of Questions
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2005, 06:47 PM
  4. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  5. A couple of Questions
    By johnnabn in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2003, 10:10 PM