Skip to content

Lab 4 Questions (8.1 to 8.5)

Glenn Lopez edited this page Mar 10, 2014 · 7 revisions

8.1 How does a pointer constant differ from a pointer variable? Give an example of each from the program in this lab.

If a pointer is an address, then we can say that a pointer variable is just a variable that can store an address. A pointer constant is a pointer that cannot change the address that its holding. Once a pointer constant points to a variable then it cannot point to any other variable.

8.2 How does mystr differ from mystr[2]? What data type is each variable?

mystr is an array of characters. In some ways it will behave like a pointer to the first item in the array, but it is genuinely a distinct type. mystr[2] has type char - it is the char found at index 2 in the array (i.e. the third char since array indices start at 0).

8.3 What is wrong with this assignment?: p_mystr = mystr[2];

In the real world: There is nothing wrong with p_mystr = mystr - it is safe to assign from a char-array to a char-pointer as long as you don't have a condition where you are making "address container"="data". p_mystr = mystr[2] basically means to make the pointer point to the first item in the array" (this question makes no sense and needs to be worded properly)

8.4 Why is this assignment correct?: *p_mystr= mystr[2];

*p_mystr=mystr[2] means something else - * operator means "the value pointed to", so what this means is "in the memory address where p_mystr is pointing, copy the character mystr[2]". This will work becuase you are setting a pointer variable on a pointer constant.

8.5 Define the following terms: source code, debug, pointer, array element, string constant.

Soure code('sauce'): Is a list of commands to be compiled written in proper syntax with respect to the type of language the compiler is made to compile.

Debug: Every time you write code you leave crumbs. These crumbs attract bugs in your software. Bugs create problems because they change how your software is intended to work when they eat through pretend wires. To debug means to fix problems in your software by cleaning up the crumbs you leave behind (looking for these crumbs is another issue, the best way to fix this problem is to learn how to comment what you wanna do before writing your code).

Pointer A pointer is a variable whose value is the address of another variable.

Array Element Each object in an array is called an array element. For example, you could have an array of integers or an array of characters or an array of anything that has a defined data type.

String Constant In C, an array of type char is used to represent a character string, the end of which is marked by a byte set to 0 (also known as a NUL character).

Clone this wiki locally