CSC161 2011S Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Reading: K&R 5.3-5.4
Please submit a question by 9 p.m. on Tuesday, 15 March 2011.
At the very end of 5.3, they talked about using code like a[-2]. Would accomplishing this require using a pointer at index 2 or greater of an array to initialize a new array a? How could this be useful in programming towards a specific goal?
Could you provide an example of when p[-1] or p[-2] would be useful/legal?
It's rarely useful. On occasion, you've advanced a pointer in an array and want to look backwards.
If it is not possible to assign one pointer to a pointer of a different type, then is it still okay to assign two pointers of identical types?
Yes. You can even force conversion.
int *ip; float *fp; ip = (int *) fp;
How consistent is C when dealing with the address of non-array variables? It obviously doesn't guarantee anything with non-array variables, but would the code:
int a;
int b;
printf ("%d\n", &b - &a);
usually return 1, or return some really strange number, or die horribly (or is it fairly random which of these happens?)
That code will almost always return 4 or -4, depending on the design of the C compiler and machine. But it should return the same value on the same machine most of the time.
Is an error automatically returned if you try to refer to objects outside of the array (since K&R say it is illegal) or will it return "junk"?
We've already seen that it often returns junk. Sometimes it causes a segmentation fault. It depends on where in memory the stuff is.
How are arrays initialized? For example, when we declare a new array
int array[5];
What are the default values of array[0], array[1], array[2], etc.?
There are no default values. You can, however, initialize an array with an array initializer.
int array[5] = { 32, 42, 11, 17, 7 };
I don't understand the "a pointer is a variable and an array name is not" part.
You can assign new values to pointers. You cannot assign new values to arrays (although you can assign new values to the cells of the array).
Why can you increment a but not pa?
What does this diagram of pointers mean ? file:///tmp/evince-21230/image.YSR1RV //*Pg 98, the last diagram.*//
I can't read files that are on your computer. But we'll go over this diagram in class.
Why it is illegal to refer to objects that are not within the array bounds?
Because it's meaningless.
Why are pointers and integers not interchangeable?
They may not be the same size. For example, on some compilers, integers are 16 bits but pointers are 32 bits. On others, integers are 32 bits but pointers are 64 its.
What is it that keeps you from adding two pointers together?
Mostly that it's a meaningless operation.
The reading states that *(a+i) is the same as a[i]. When would one form be used over the other? Is there a specific standard?
You use whichever one seems clearer (typically the array notation). Custom is all that dictates the choice.
What does malloc do?
Allocates memory dynamically (on the fly) so that you can continue to use it.
What does char *s[] mean?
s is an array ([]) of strings (char *).
When the text says "" so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal"" is it saying that the pointer can be in an array and increment, but an array cannot be in a pointer and cannot increment?
How does allocbuf work?
The reading says that
int a[];
is the same as
int *a;
However later on the book says that you cant do certain operations with array names. This seems highly contradictory. The book also mentions that functions treat array names passed to it as local pointers. Is that true if you use functions with headers like
void do_stuff(int arr[])
as opposed to
void do_stuff(int *arr)
and pass it the same array?"
If pointer can be manipulated by using pa=a or pa++ then what's the difference between a counter variable like int i; that could be used to go through the entire array? Is it doing the same thing?
And what does it mean to declare something static? And why is zero never a valid address? If you use an array pointer to refer to a point in memory which is not within the legal bounds of the array, will C allow you to access that point in memory anyway, or will you receive errors or nonsense? "If char s[] and char *s are equivalent, have we been using pointers the whole time we've been using arrays?" "Is there a possibility of accidentally breaking a computer with a run away script that increments/decrements a pointer endlessly?
int a = 0; int *i = &a; // increment int *d = &a; // decrement
while (a == 0) { i++; d++;
I guess this also goes into where exactly in the computer's memory is all this bits, bytes, and pointers being stored."
On page 99, why is construction pa=a legal for a pointer but not for an array?
What does the code on page 101 and 102 do exactly? I don't really follow on how it gets through the code.
Can you explain what "static" means and what it is used for?
In the 1st full paragraph on page 103, they mention something about n being scaled based on the size of the objects p points to. It says that if int is four bytes, the int will be scaled by four. What does this mean?
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Wed Mar 16 10:46:16 2011.
The source to the document was last modified on Wed Mar 16 10:46:15 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/pointers-revisited.html.