|
-
June 5th, 2002, 08:29 AM
#1
C Exam questions
A friend of mine (yes really!) is looking doing a C course, and has had the following exam questions amongst others.
1) What is the difference between:
a) char *p[];
b) char (*p)[];
2) Can a char[4] contain the string "main"?
Question 1a is an array of char pointers, I think 1b is a pointer to an array. Do you agree?
Question 2 I feel is a lot more contentious. The exam paper wanted a one word answer (yes/no) I believe the answer is vague. It is no if you want to store a null termintated string, but strictly speaking the 4 characters can be held in an array of four chars especially if you know the lenght of the array is 4.
There are also another 30 off questions but these are relatively simple.
-
June 5th, 2002, 10:26 AM
#2
Re: question 2 - the answer is "No", because "main" (i.e. the four chars 'm', 'a', 'i' and 'n' surrounded by double quotes) is a string literal and these are always null-terminated, so a four-character array can't hold it.
In other words:
Code:
char text[4];
strcpy(text, "main");
will fail.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 5th, 2002, 10:38 AM
#3
and using strncpy(text,"main"); exposes you to buffer overflow
-
June 5th, 2002, 05:08 PM
#4
I love exams...
>> 1) What is the difference between:
>> a) char *p[];
>> b) char (*p)[];
Trick question. There is no difference if the compiler takes the command. The answer is both allocate a character pointer to a character pointer. Because an array is just a pointer with memory associated to it.
char szBuffer[100] = "";
is equivelent to
char *pszBuffer = malloc ( 100 );
pszBuffer[0] = '\0'; <- addressing a pointer as an array
char *p; allocates a pointer of type char.
char p[10] you learn allocates an array, but in reality it creates a pointer p and associates 10 bytes with that pointer.
Thus "char p[]" syntax, which isn't accepted by every compiler.... VC++ for example didn't used to support this syntax, an array syntax with no size element, allocates a pointer with no memory associated with it just like char *p.
So
char *p = "blah blah blah"; <-- allocates a pointer and points it to the static string "blah blah blah"..
char p[] = "blah blah blah"; <- does the exact same thing
in both cases p can be read as a string.
Thus char *p[] is equivelent to char **p or char (*p)[];
No difference but this is really really trivia your professor must be one anal dude.
Not to beat this subject to death but... what if you subsitueted a number here..
>> 1) What is the difference between:
>> a) char *p[10];
>> b) char (*p)[10];
In this case it is clear the result in each case is an array of 10 char pointers.
>> Question 2 I feel is a lot more contentious.
The answer to question 2 is No. Four characters as you say can certainly be stored in a four byte buffer but that by definition is not a string but a character array. The question was can this "String" be stored in this variable and if you don't include the null terminator by definition you didn't get the entire string!
-
June 6th, 2002, 10:10 AM
#5
Hello!
There is a difference in question 1:
a.) array of pointers
b.) pointer to an array
BIG DIFFERENCE!
void*
-
June 6th, 2002, 03:12 PM
#6
Let me blow your mind....
>> There is a difference in question 1:
>> a.) array of pointers
>> b.) pointer to an array
>> BIG DIFFERENCE!
No Difference.... follow me here cause this get's convoluted..
char *a[] you say is an arrray of zero ponters. I say your right, but I say this is exactly the same as a pointer to a pointer.
char (*a)[] you say is a pointer to an array of zero elements. I say that you're right again, but that is also a pointer to a pointer.
An array is a pointer with memory associated with it. Or a pointer with memory associated with it is an array.... They are the same and can be treated as such.. Likewise an array with no memory associated with it is merely a pointer... Check it out...
// I define a structure
typedef struct __MYSTRUCT
{
int a;
int b;
} *PMYSTRUCT, MYSTRUCT;
void main()
{
// declair variables
MYSTRUCT *pPointer = NULL;
MYSTRUCT Structure[100];
// associate memory with my pointer
pPointer = new MYSTRUCT[100];
// Loop through and initialize my arrays..
for( int i = 0; i < 100; i++)
{
pPointer[i].a = 1; // <- pointer is an array
Structure[i].a = 1; // <- array is an array
(pPointer+i)->b = 2; // <- pointer is also a pointer
(Structure+i)->b = 2; // <- array is also a pointer
}
}
Does that blow your mind or what? An Array is just a pointer with memory associated with it. Character arrays are the same.
>> char a[];
An array with no memory associated with it is equal to a pointer with no memory associated with it.
The '*' is a derefference syntax.
The '[n]' syntax adds n onto the pointer and then derrefferences. If their is no n the '[]' is equivelent to the '*' command.
Think about it, it's a powerful technique for refferencing dynamic data structures.
Last edited by JMS; June 6th, 2002 at 03:33 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|