related to k&R 5.7

M

mdh

A Happy Easter to all.


#include <stdio.h>

static char mnths[] = {0,1,2,3,4,5,6,7,8,9,10,11,12};

int main () {
int i;
for (i=1; i<=12; i++)
/**printf("%d\n", mnths); works**/
printf("%d\n", *mnths++); **/<<<<error:wrong type arg to increment**/
return 0;
}


Could someone perhaps enlighten me...I thought an array was passed as
a pointer...but obviously, am missing something.

Thanks in advance.
 
K

Kelsey Bjarnason

A Happy Easter to all.


#include <stdio.h>

static char mnths[] = {0,1,2,3,4,5,6,7,8,9,10,11,12};

int main () {
int i;
for (i=1; i<=12; i++)

A tad unusual, that. Normal would be to loop from 0 to < n, rather than
from 1 to n. I assume there's a reason for using the initial 0 in the
months array and looping this way, rather than the normal way.
/**printf("%d\n", mnths); works**/
printf("%d\n", *mnths++); **/<<<<error:wrong type arg to increment**/
return 0;
}


Could someone perhaps enlighten me...I thought an array was passed as
a pointer...but obviously, am missing something.


That's not quite the problem here. Consider the following:

char *src = somevalue, *dst = buffer;

while( *src )
*dst++ = *src++;

What, exactly, does *src++ do? It gets the value of *src (i.e. pulls
a character out of the source buffer) then increments the pointer src.
Useful in, say, a string copy type of routine.

Now look at your code:

static char mnths[] = {...};
printf("%d\n", *mnths++);

So what does *mnths++ do? It gets the value of *mnths (i.e. pulls a
character out of the buffer) and then increments mnths.

Whoops. mnths is an array. You can't increment an array.

The fact that printf, as a function, sees a "pointer" is irrelevant; the
increment sees the actual array, and you're trying to increment the array,
which won't work.
 
A

Army1987

mdh said:
A Happy Easter to all.


#include <stdio.h>

static char mnths[] = {0,1,2,3,4,5,6,7,8,9,10,11,12};

int main () {
int i;
for (i=1; i<=12; i++)
/**printf("%d\n", mnths); works**/
printf("%d\n", *mnths++); **/<<<<error:wrong type arg to increment**/
return 0;
}


mnths is an array, and you can't increase it.

You don't pass an array to printf, you pass an element of it (mnths not
mnths) (except in the case of strings, but mnths is not a string as it
doesn't end with 0).

That would work if you wrote:

#include <stdio.h>
void print_arr(char arr[], int length)
{
for (i=0; i<length; i++) /* In C, an array of N elements is numbered
from 0 to N-1. Here it doesn't matter, because you don't use array subscript
notation, but usually in C cycles are always for (i=0; i<n; i++) */
printf("%d\n", *arr++);
}
int main(void)
{
print_arr(mnths+1, 12);
return 0;
}

BTW, why do you declare the elements of mnths as char, if you only use them
as numbers and never as characters? In general, it isn't a good idea: int
are faster, even if they occupy more memory. You only use e.g. 26 or 52 (on
most systems) bytes instead of 13, but this isn't a great deal. See
http://c-faq.com/decl/inttypes.html, second paragraph.
 
A

Army1987

Army1987 said:
mdh said:
static char mnths[] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
You don't pass an array to printf, you pass an element of it (mnths not
mnths) (except in the case of strings, but mnths is not a string as it
doesn't end with 0).


Ehm...
It *is* a string. An empty string, exactly, as it contains no character
before 0.
But you're not using it that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

K&R 5.7 1
page 120 K&R 46
K&R 1-24 15
p124 K&R 10
K&R Exercise 1-21: entab 10
Comparison of Integer and Pointer (that's supposed to be an Integer). Where did I go wrong? 0
K&R xrcise 1-13 6
K&R exercise 4-12 5

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top