Question:
C programming questions?
- DP -
2009-05-12 08:47:16 UTC
1. How is the length of a string determined in C ?
[a.] From the declared size of the array which contains it.
[b.] By a length byte stored at the beginning of the string.
[c.] By a null terminator stored at the end of the string.
[d.] By a length byte stored at the end of the string.

My thoughts: I think this one is C because the null terminator determines where the string ends?

2. Given the following declaration,
char string[8] = "abcdefg"; what is output by the following statement: printf ("%s\n", string + 3);

[a.] abcdefg
[b.] abc
[c.] defg
[d.] cdefg

My thoughts: I think this one is C because the string was shifted 3, but I'm not sure.

3. The following code char string[8] = "abcdefg"; *string = '\0'; printf ("%s", string);

[a.] generates a compiler error
[b.] generates a run-time error
[c.] creates no output, but no error is generated
[d.] creates the output bcdefg

My thoughts: I think this one is C because the string's first location is changed to a null terminator, therefore the string is empty now?

4. Let p be a pointer to an integer and num be an integer variable. Which of the following is NOT a correct assignment ?

[a.] p = 0;
[b.] p = 5;
[c.] p = p + 7;
[d.] p = #

My thoughts: I'm not sure about this one. They all seem valid to me.

5. Let p be a pointer to an integer and let n be an int variable. Then after the assignment p = &n;
the value of *p is

[a.] the address of the pointer p
[b.] the value stored in the variable n
[c.] the address of the variable n
[d.] none of the above

My thoughts: I think it would be b because the *p is the dereference of the address, so it would actually be the value?

6. The arrow operator, ->, has the effect of which other two operators ?

[a.] * and ++ (dereference and then increment pointer)
[b.] - and > (decrement and then check for greater-than
[c.] [] and . (get array element and then get member)
[d.] * and . (dereference and then get member)

My thoughts: I think the answer is d, but I'm not 100% sure.


7. The expression sizeof(struct foo) refers to

[a.] the number of member variables in struct foo
[b.] the size of the largest member variable in struct foo
[c.] the total size of struct foo in bytes
[d.] the number of pointer members in struct foo

My thoughts: I'm actually not sure. I think it's C, but I'm not confident in my answer.

8. # What is the effect of dereferencing a pointer which has the value NULL ?

[a.] Zero is returned.
[b.] The operation is ignored.
[c.] The compiler detects the error and issues an error message.
[d.] A run-time error results.

My thoughts: I think the answer would be D because the NULL makes the program give an error?

Please answer and explain!

Thank you!
Four answers:
femtorgon2
2009-05-12 09:48:10 UTC
1 - c

Yes, the length of the string is generally determined by where the null terminator occurs. However, this is a bit of a confusing question, since the size of the string would be determined by a. For instance, if I create a string as:

char name[20];

name = "Joe"

name would still be allocated as an array of 20 characters, even though only 4 are used and would be considered as the length (ie, strlen would return 3). So, it depends on what we mean by "length".



2 - c

Yes, since the string is a pointer, adding 3 will move the pointer to the fourth address in the array (index: 3), then printf will output from there to the null terminator.



3 - c

Precisely right on that one.



4 - b

Most compilers will certainly throw an error at you here. It is at best a very unusual practice to define an address using an int literal. Try it out, you should get an invalid conversion error. However, a, setting it to 0 is a special case, of essentially setting the pointer to NULL. You can, of course, force this conversion.

c and d are valid because your working from known addresses, so arithmetic from it may be meaningful.



5 - b

Precisely right.



6 - d

Correct.

p->num

is identical to

(*p).num

I'm fairly sure the compiler will interpret both as identical statements, and there will be no difference whatever in how they are interpreted.



7 - c

It will give you the total size in bytes. Of course, if your struct holds a pointer to a big array somewhere, this will only represent the size of the pointer, not the array.



8 - d

Yes, this will cause a segfault. NULL is just the address 0. It is not a valid address, so it can not be dereferenced.
2009-05-12 09:13:31 UTC
Your answers all look ok to me, for Q4 I would say:



[b.] p = 5;



is wrong, mainly because the others all seem like valid expressions,



[a.] p = 0; // == Assign NULL to p

[c.] p = p + 7; // == increment p by 7, like stepping over an array

[d.] p = # // == put the address of num into p



but [b] is saying "put the memory address 5 in p" which could work but I think compilers protect the first so many bytes of RAM as it's allocated to the system. and assigning a constant into a pointer might not compile either. I've never tried myself, it just looks wrong!
Graham
2009-05-12 08:59:47 UTC
Looks like homework...

1. You're correct.

2. You're correct. It's pointer arithmetic.

3. You're correct.

4. The answer is B. The only constant that can be assigned to a pointer is 0 (NULL).

5. You're correct. Pointers contain addresses; the operator * dereferences them to the value there..

6. You're correct. a->b is the same as (*a).b

7. You're correct. Sizeof always returns the amount of bytes the entity given takes up. (It's a unary operator, actually, for that purpose.)

8. You're correct. It will be a General Protection Exception (Segfault).
2016-10-04 08:44:45 UTC
NeilLinux2010:~$ vi maxminnumbers.cpp NeilLinux2010:~$ g++ -g -O2 maxminnumbers.cpp -o maxminnumbers NeilLinux2010:~$ ./maxminnumbers 3 8 22 19 a million EOF detected--dumping outcomes max=22 min=a million NeilLinux2010:~$ right here is the code: /* maxminnumbers.cpp objective: examine the familiar enter for integers, checklist the optimal and minimum values, then checklist them on the top whilst end of stdin is reached. 2010-02-01 */ #contain iostream // for cin, cout #contain cstdio // for feof, stdin using namespace std; int significant(int argc, char **argv) { int cmax, cmin, num; // decl of vars cin >> num; // get the 1st selection to initialize each and all of the others. cmax = cmin = num; // init all different vars to same cost on a similar time as (!feof(stdin)) { cin >> num; // get different numbers till end of report (feof()) if (num>cmax) { cmax = num; // a sparkling optimal replaced into detected. } else if (num
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...