#include #include #include #define ROWS 3 #define COLS 4 /***** ** Exercise 1: ** What is sizeof(matrix)? How does the memory look? ** ** Exercise 2: ** Find out the number of command line arguments. Print the length of ** each string? ** ** Exercise 3: ** Assume matrix is declared as: ** char matrix[4][3]; ** 1. What happens if you copy a string with COLS characters to ** matrix[0]? ** 2. What happens if you copy COLS+1 to matrix[0]? Is it legal (as in, does ** the memory spill over and overwrite some other variable) ? ** 3. What happens if you copy COLS+1 to matrix[3]? Is it legal? ** ** Show the memory allocation in the above cases and check them by ** writing a program? ** ** Exercise 4: ** Let ** char* ptrToStr; ** Point ptrToStr to matrix[0]. Print ptrToStr. What happens if we do ** ptrToStr++? Print ptrToStr now. ** ** Exercise 5: HARD ** Let ** char** ptrTo2DStr; ** Point ptrTo2DStr to matrix (cast it explicitly). What happens if we do ** ptrTo2DStr++? Print ptrTo2DStr. Is there anything wrong? ** *****/ int main(int argc, char *argv[]) { char matrix[ROWS][COLS]; /* what does memory look like? */ char **ptrTo2DStr; int i; scanf("%s", matrix[1]); /* if the user enters "cat", what happens? */ strcpy(matrix[2], "dog"); /* and now? */ for (i = 0; i < ROWS; i++) printf("string in row %d: %s\n", i, matrix[i]); return 0; }