using strcpy to copy from a char

A

Alok Kumar

#include <string.h>
void myfn()
{
char a = 'A';
char b[2];
strcpy(b, &a);
}

Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
 
G

Guest

Alok said:
#include <string.h>
void myfn()
{
char a = 'A';
char b[2];
strcpy(b, &a);
}

Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?

No, you can't safely assume that. strcpy will try to read *(&a + 1)
and compare it to '\0', but the byte after a isn't necessarily yours
to read, and even if it is, it's not sure to be 0.
 
M

Malcolm McLean

Alok Kumar said:
#include <string.h>
void myfn()
{
char a = 'A';
char b[2];
strcpy(b, &a);
}

Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
No. strcpy will copy values until it hits a zero byte. On the first run that
will quite likely be the b[0], and the function will appear to work as you
want. However eventually the following byte may not be zero, and the
fucntion will plough through memory it doesn't own with unpredictable
consequences.
 
K

klaushuotari

#include <string.h>
void myfn()
{
char a = 'A';
char b[2];
strcpy(b, &a);

}

Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?

Not likely. 'A' is not terminated by NULL byte. It's just a char
constant.

If you would put it like this:

#include <string.h>
void myfn()
{
char a = "A";
char b[2];
strcpy(b, &a);

}

You would get "A" always in b array.
 
C

christian.bau

#include <string.h>
void myfn()
{
char a = 'A';
char b[2];
strcpy(b, &a);

}

Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?

Each and every time while you are developing your program.
As soon as the program is used seriously and failure would cost you
money, your program will crash.
 
B

Barry

#include <string.h>
void myfn()
{
char a = 'A';
char b[2];
strcpy(b, &a);

}

Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?

Not likely. 'A' is not terminated by NULL byte. It's just a char
constant.

If you would put it like this:

#include <string.h>
void myfn()
{
char a = "A";
char b[2];
strcpy(b, &a);

}

You would get "A" always in b array.

You need a compiler.
 
K

klaushuotari

#include <string.h>
void myfn()
{
char a = 'A';
char b[2];
strcpy(b, &a);
}
Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
Not likely. 'A' is not terminated by NULL byte. It's just a char
constant.
If you would put it like this:
#include <string.h>
void myfn()
{
char a = "A";
char b[2];
strcpy(b, &a);

You would get "A" always in b array.

You need a compiler.- Piilota siteerattu teksti -

- Näytä siteerattu teksti -

I think you're right.
 
W

websnarf

#include <string.h>
void myfn() {
char a = 'A';
char b[2];
strcpy(b, &a);
}

Would I always get 'A' in b[0] and '\0' in b[1] after the
strcpy?

Welcome to the lack of type safety that is the C language.

Although you made the variable a into a char *, you did not make it a
string. So when you pass its address as the second parameter to
strcpy, you are actually invoking "undefined behavior". Taking the
address of a char does not make it a "string" in the sense of the C
language. Specifically a which is 'A' is not necessarily followed by
a '\0'. (To be a string in C, you have to be a sequence of characters
terminated by a '\0'.)

The language lets you compile and even try to run this, but it doesn't
make any sense. The reason is that strings in C are defined
semantically, not syntactically. (And there is no type checking for
semantics in C.)
 
C

CBFalconer

#include <string.h>
void myfn() {
char a = 'A';
char b[2];
strcpy(b, &a);
}
.... snip ...

Although you made the variable a into a char *, you did not make
it a string. So when you pass its address as the second parameter
to strcpy, you are actually invoking "undefined behavior". Taking
the address of a char does not make it a "string" in the sense of
the C language. Specifically a which is 'A' is not necessarily
followed by a '\0'. (To be a string in C, you have to be a
sequence of characters terminated by a '\0'.)

Pedantically wrong. a is perfectly capable of holding a string, as
long as that string does not exceed zero length. As initialized, a
is not a string. Char arrays can hold strings. An empty string is
not an empty char array.
 
K

Keith Thompson

Malcolm McLean said:
Alok Kumar said:
#include <string.h>
void myfn()
{
char a = 'A';
char b[2];
strcpy(b, &a);
}

Would I always get 'A' in b[0] and '\0' in b[1] after the strcpy?
No. strcpy will copy values until it hits a zero byte. On the first
run that will quite likely be the b[0], and the function will appear
to work as you want. However eventually the following byte may not be
zero, and the fucntion will plough through memory it doesn't own with
unpredictable consequences.

It's much worse than that. There's no reason to assume that b[0] is
initially equal to '\0', or that b immediately follows a in memory.

I just tried the above function with a few printf() statements added.
The initial value of b[0] was 100, and "a" *followed* b in memory,
after a 1-byte gap. (The strcpy() call caused a segmentation fault.)
 
M

Malcolm McLean

CBFalconer said:
Pedantically wrong. a is perfectly capable of holding a string, as
long as that string does not exceed zero length. As initialized, a
is not a string. Char arrays can hold strings. An empty string is
not an empty char array.
Is one sausage a string of sausages?
 
C

Chris Torek

.... pointed out that a single "char" can hold a C string, as long as
the string is the empty string.

Is one sausage a string of sausages?

Yes. In fact, "no sausages" is also a string of sausages. :)

(Both are "degenerate cases", and if this were mathematics, one
would just specify "n >= 2" or "excluding degenerate cases" or
whatever.)

Or, in other words, mathematicians are odd. (Except when even,
complex, or irrational.) (I started college in a "math for people
who want to get a PhD in math and become a Math Professor at a
university" course, but ended up doing CS instead: it was easier,
more fun, and way more profitable. :) )
 
M

Malcolm McLean

Chris Torek said:
... pointed out that a single "char" can hold a C string, as long as
the string is the empty string.



Yes. In fact, "no sausages" is also a string of sausages. :)

(Both are "degenerate cases", and if this were mathematics, one
would just specify "n >= 2" or "excluding degenerate cases" or
whatever.)

Or, in other words, mathematicians are odd. (Except when even,
complex, or irrational.) (I started college in a "math for people
who want to get a PhD in math and become a Math Professor at a
university" course, but ended up doing CS instead: it was easier,
more fun, and way more profitable. :) )
I am plannig to design a new language. It is going to be largely graphical
rather than in traditional text source.

To give you a bit of backgound, my current idea is to have two atomic data
types, a real and a "symbols". Symbols will be 64-bit values consisting of
a type and an index. Type 1 is the natural integers, of course, whilst type
0 is the list of symbol types themselves.

One thing I am try to work out is how to best represent a "wire" carrying no
signal rather than a wire carrying a signal of zero. On option is to se the
first wire to nan, the other is not to distinguish between zero and no
signal. Ditto with symbols, 0-0 can be the null symbol, but do I need
another null to represent no nothings?
 
R

Richard Tobin

Is one sausage a string of sausages?

Yes. In fact, "no sausages" is also a string of sausages. :)

(Both are "degenerate cases", and if this were mathematics, one
would just specify "n >= 2" or "excluding degenerate cases" or
whatever.)[/QUOTE]

Is this were mathematics, we would distinguish between sausage and
{sausage}. Or, since we want things ordered, between sausage and
(sausage). C has an automatic conversion between (spam eggs sausage ...)
and &spam, so you can use &sausage in the same contexts as (sausage).
Or, in other words, mathematicians are odd.

You should hear what mathematicians say about computer scientists.

-- 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
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top