Any help?

C

cj

Can someone help me with this question? I think I've narrowed it down
to 2 out of the 4 but not sure.
char array [80] = " I am short";
Which would be true? The size of the array would be reduced to 10 or
the array size stays at 80 and the string fills array locations,
starting at array [0].
 
T

tedu

cj said:
Can someone help me with this question? I think I've narrowed it down
to 2 out of the 4 but not sure.
char array [80] = " I am short";
Which would be true? The size of the array would be reduced to 10 or
the array size stays at 80 and the string fills array locations,
starting at array [0].

why not find out?

#include <stdio.h>
int main(int argc, char **argv) {
char array [80] = " I am short";
printf("%lu\n", (unsigned long)sizeof(array)); /* best format for
c89 */
return 0;
}
 
V

Vladimir S. Oka

tedu said:
cj said:
Can someone help me with this question? I think I've narrowed it down
to 2 out of the 4 but not sure.
char array [80] = " I am short";
Which would be true? The size of the array would be reduced to 10 or
the array size stays at 80 and the string fills array locations,
starting at array [0].

why not find out?

#include <stdio.h>
int main(int argc, char **argv) {
char array [80] = " I am short";
printf("%lu\n", (unsigned long)sizeof(array)); /* best format for
c89 */
return 0;
}

Wouldn't it be better to learn _why_ this is so, instead of just
demonstrating the final answer?

The line

char array[80] = " I am short";

reserves 80 bytes for use by the array. These can be filled in any way
imaginable. It also assigns the characters ' I am short' followed by a 0
starting with array[0]. The rest is unused, but can be if need be.

Character arrays in C are not "strings". C does not have a "string"
type. By convention (as recognised by numerous standard library
functions) "strings" are just arrays of characters terminated by a zero
character. Literal strings (the ones surrounded by double quotes) are
just convenient way to express this without actually typing the
terminating zero -- that is implied.

I'm sure there are better (more PC?) ways of saying this, but I think
it's still more helpful than just demonstrating the after-effects.

Cheers

Vladimir
 
P

pete

cj said:
Can someone help me with this question? I think I've narrowed it down
to 2 out of the 4 but not sure.
char array [80] = " I am short";
Which would be true? The size of the array would be reduced to 10

No.

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
char array [80] = " I am short";

printf("sizeof array is %u\n", (unsigned) sizeof array);
puts(array);
return 0;
}

/* END new.c */
or
the array size stays at 80 and the string fills array locations,
starting at array [0].

Yes, and the rest of the array elements following the string,
are initialized to zero.
 
P

pete

Vladimir said:
cj said:
Can someone help me with this question? I think I've narrowed it down
to 2 out of the 4 but not sure.
char array [80] = " I am short";
Which would be true? The size of the array would be reduced to 10 or
the array size stays at 80 and the string fills array locations,
starting at array [0].

why not find out?

#include <stdio.h>
int main(int argc, char **argv) {
char array [80] = " I am short";
printf("%lu\n", (unsigned long)sizeof(array)); /* best format for
c89 */
return 0;
}

Wouldn't it be better to learn _why_ this is so, instead of just
demonstrating the final answer?

The line

char array[80] = " I am short";

reserves 80 bytes for use by the array. These can be filled in any way
imaginable. It also assigns the characters ' I am short'
followed by a 0

Followed by how many 0?
starting with array[0]. The rest is unused, but can be if need be.
 
E

Eric Sosman

Vladimir S. Oka wrote On 01/19/06 15:08,:
tedu said:
cj said:
Can someone help me with this question? I think I've narrowed it down
to 2 out of the 4 but not sure.
char array [80] = " I am short";
Which would be true? The size of the array would be reduced to 10 or
the array size stays at 80 and the string fills array locations,
starting at array [0].

why not find out?

#include <stdio.h>
int main(int argc, char **argv) {
char array [80] = " I am short";
printf("%lu\n", (unsigned long)sizeof(array)); /* best format for
c89 */
return 0;
}


Wouldn't it be better to learn _why_ this is so, instead of just
demonstrating the final answer?

The line

char array[80] = " I am short";

reserves 80 bytes for use by the array. These can be filled in any way
imaginable. It also assigns the characters ' I am short' followed by a 0
starting with array[0]. The rest is unused, but can be if need be.

The "unused" portion of the array will be filled
with zero bytes. Initialization is not the same as
strcpy().

(This seems to be my day for picking on Vladimir;
I hope he doesn't think I'm persecuting him ...)
 
M

Mike Wahler

cj said:
Can someone help me with this question? I think I've narrowed it down
to 2 out of the 4 but not sure.
char array [80] = " I am short";
Which would be true? The size of the array would be reduced to 10

False. The array's size is 80 bytes.
or
the array size stays at 80

Correct. Once an array has been created, it's
size cannot be changed.
and the string fills array locations,
starting at array [0].

The string (including its terminator) occupies elements 0 through 11,
elements 12 through 79 are initialized with zero.


-Mike
 
V

Vladimir S. Oka

Eric said:
Vladimir S. Oka wrote On 01/19/06 15:08,:
tedu said:
cj wrote:

Can someone help me with this question? I think I've narrowed it down
to 2 out of the 4 but not sure.
char array [80] = " I am short";
Which would be true? The size of the array would be reduced to 10 or
the array size stays at 80 and the string fills array locations,
starting at array [0].
why not find out?

#include <stdio.h>
int main(int argc, char **argv) {
char array [80] = " I am short";
printf("%lu\n", (unsigned long)sizeof(array)); /* best format for
c89 */
return 0;
}

Wouldn't it be better to learn _why_ this is so, instead of just
demonstrating the final answer?

The line

char array[80] = " I am short";

reserves 80 bytes for use by the array. These can be filled in any way
imaginable. It also assigns the characters ' I am short' followed by a 0
starting with array[0]. The rest is unused, but can be if need be.

The "unused" portion of the array will be filled
with zero bytes. Initialization is not the same as
strcpy().

(This seems to be my day for picking on Vladimir;
I hope he doesn't think I'm persecuting him ...)

Of course you are... You ALL are... EVERYBODY IS!!! ;-)

'Course not, and 'course you're right, now that I employed more than 11
gray cells. Please feel free to correct as and when needed.

NB, I'm not trying to dig myself out of the hole here:

In the context of OP's original question, which was:
> Which would be true? The size of the array would be reduced to 10 or
> the array size stays at 80 and the string fills array locations,
> starting at array [0].

I'd still say that array elements past the first 0, are "unused", i.e.
the array stays the size 80 _and_ the string fills its locations
starting with array[0].

Cheers

Vladimir
 
K

Keith Thompson

Vladimir S. Oka said:
In the context of OP's original question, which was:
Which would be true? The size of the array would be reduced to 10 or
the array size stays at 80 and the string fills array locations,
starting at array [0].

I'd still say that array elements past the first 0, are "unused",
i.e. the array stays the size 80 _and_ the string fills its locations
starting with array[0].

If you use the array as a string (e.g., by passing the address of its
first element to printf() or strlen()), the array elements past the
first 0 will be unused. But since the language guarantees that all
those elements will be initialized to '\0', a program can legally use
them, and can depend on their initial values. (Not any program that
I'd wrote, though.)
 
H

Haroon Shafiq

Keith said:
Vladimir S. Oka said:
In the context of OP's original question, which was:
Which would be true? The size of the array would be reduced to 10 or
the array size stays at 80 and the string fills array locations,
starting at array [0].

I'd still say that array elements past the first 0, are "unused",
i.e. the array stays the size 80 _and_ the string fills its locations
starting with array[0].

If you use the array as a string (e.g., by passing the address of its
first element to printf() or strlen()), the array elements past the
first 0 will be unused. But since the language guarantees that all
those elements will be initialized to '\0', a program can legally use
them, and can depend on their initial values. (Not any program that
I'd wrote, though.)

why don't we check this with a program too? :)

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char array [80] = " I am short";
int i = 0;
for (; i < 80; ++i)
{
if (array == '\0')
{
printf ("\\0");
}
else
{
printf ("%c", array);
}
}
return EXIT_SUCCESS;
}
 
S

Sriniv

pete wrote:

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
char array [80] = " I am short";

printf("sizeof array is %u\n", (unsigned) sizeof array);
puts(array);
return 0;

}

<snip>

Why is the typecast to unsigned inside the printf needed? We specify a
%u there and also sizeof operator returns an unsigned, right? If so,
why do we do an explicit typecast there? If aint so, the reason please.

Thank you all.

-- Sriniv.T
 
C

CBFalconer

Sriniv said:
pete wrote:

#include <stdio.h>

int main(void)
{
char array [80] = " I am short";

printf("sizeof array is %u\n", (unsigned) sizeof array);
puts(array);
return 0;
}

<snip>

Why is the typecast to unsigned inside the printf needed? We
specify a %u there and also sizeof operator returns an unsigned,
right? If so, why do we do an explicit typecast there? If aint
so, the reason please.

It ain't so. sizeof returns a size_t. In this case we can be
fairly sure that the magnitude fits into an unsigned int.

Passing a size_t to a C89 printf is one of the rare cases when a
cast is required.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
R

Richard Bos

Sriniv said:
pete wrote:
Why is the typecast to unsigned inside the printf needed? We specify a
%u there and also sizeof operator returns an unsigned, right? If so,
why do we do an explicit typecast there? If aint so, the reason please.

No, sizeof evaluates to a size_t, which is an unsigned type but not
necessarily an unsigned int. It may be wider (or even narrower, though
that would be highly unusual).

If you didn't know the size of the array even approximately and wanted
to make sure of the widest possible range, you'd use %lu and (unsigned
long); if you were using C99, you'd use %zu.

Richard
 

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

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top