MC logo

Pointers and Strings

  Practice Questions

What is printed by each piece of code?
  1. 
            char fred[50];
    
            strcpy(fred, "Today");
            if (fred == "Today")
                    printf("Of course!\n");
            else
                    printf("Today is not Today on planet C.\n");
    

  2. 
            char fred1[] = "barney", fred2[20] = "barney", *fred3 = "barney";
    
            printf("%d %d %d %d %d %d\n", 
                    strlen(fred1), strlen(fred2), strlen(fred3), 
                    sizeof fred1, sizeof fred2, sizeof fred3);
    

  3. 
            char sue[] = "susan", *p;
    
            for(p = sue; *p++; )
                    printf("%s", p);
            printf("\n");
    

  4. 
            char bill[50] = "william";
    
            strcpy(bill + 3, "ey coyote");
            printf("%s\n", bill);
    

  5. 
            char *sharon = "sharon";
            
            while(*++sharon) printf("%c", sharon[-1]);
            printf("\n");
    

<<Pointers and Arrays Answer Dynamic Allocation in C>>