473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Question on arrays within a struct and sprintf

aap
I have the following code
#define MAX 32
struct A
{
char carr[MAX];
int iarr[MAX];
int i;
};
void main()
{
struct A a;
strcpy(a.carr, "a.carr Test1");
char test[MAX];
strcpy(test, "2Test2");
sprintf(test, "%s", &a.carr); //method1
printf("%s\n", test);
sprintf(test, "%s", a.carr); //method2
printf("%s\n", test);
}
I use sprintf twice. In the first case I pass &a.carr as the argument
and in the second caseI pass a.carr as the argument.
In both the cases, the code works just fine.
Why does it not fail in the first case?
How come &a.carr and a.carr both produce identical results?

I apologize if this is already covered in a faq.

Thanks.

Nov 15 '05 #1
2 4694


aap wrote:
I have the following code
#define MAX 32
struct A
{
char carr[MAX];
int iarr[MAX];
int i;
};
void main()
{
struct A a;
strcpy(a.carr, "a.carr Test1");
char test[MAX];
strcpy(test, "2Test2");
sprintf(test, "%s", &a.carr); //method1
printf("%s\n", test);
sprintf(test, "%s", a.carr); //method2
printf("%s\n", test);
}
I use sprintf twice. In the first case I pass &a.carr as the argument
and in the second caseI pass a.carr as the argument.
In both the cases, the code works just fine.
Why does it not fail in the first case?
How come &a.carr and a.carr both produce identical results?

I apologize if this is already covered in a faq.


It's sort of covered by Questions 6.12 and 6.12 in
the comp.lang.c FAQ list

http://www.eskimo.com/~scs/C-faq/top.html

Check them out, and ask again if it's still not clear.
(While you're at it, see Question 11.2 as well.)

--
Er*********@sun.com

Nov 15 '05 #2
"aap" <ap*****@gmail.com> writes:
I have the following code
#define MAX 32
struct A
{
char carr[MAX];
int iarr[MAX];
int i;
};
void main()
{
struct A a;
strcpy(a.carr, "a.carr Test1");
char test[MAX];
strcpy(test, "2Test2");
sprintf(test, "%s", &a.carr); //method1
printf("%s\n", test);
sprintf(test, "%s", a.carr); //method2
printf("%s\n", test);
}
I use sprintf twice. In the first case I pass &a.carr as the argument
and in the second caseI pass a.carr as the argument.
In both the cases, the code works just fine.
Why does it not fail in the first case?
How come &a.carr and a.carr both produce identical results?

I apologize if this is already covered in a faq.


The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>.
Section 6 is probably most relevant to your problem (but read
the whole thing).

main() returns int, not void. The correct declaration (if you're not
using command-line arguments) is "int main(void)". This isn't likely
to cause visible problems in practice, but there's no reason not to do
it right. Once you fix that, you should have a "return 0;" at the end
of main (not required in C99, and not strictly required in C90, but a
good idea).

You're mixing declarations and statements in main(). This is
permitted in C99, but not in C90. (It's also permitted in C++, but
that's irrelevant unless you're using a C++ compiler.)

You need a "#include <stdio.h>" to use sprintf and printf, and a
"#include <string.h>" to use strcpy.

Your problem really has nothing to do with structures. Here's a
simplified example.

#include <stdio.h>
#include <string.h>

#define MAX 32

int main(void)
{
char target[MAX];
char source[] = "hello";
sprintf(target, "%s", source); /* ok */
printf("target = \"%s\"\n", target);
sprintf(target, "%s", &source); /* potential problem */
printf("target = \"%s\"\n", target);
return 0;
}

As you'll see when you've read section 6 of the FAQ, an array name is
implicitly converted to a pointer to its first element *unless* it's
the operand of a sizeof or unary "&" operator.

The first call to sprintf is ok. The "%s" format expects a char*
argument, and source is converted to type char* (a pointer to the
character 'h').

The second sprintf call is questionable. Because it's the operand of
a unary "&", source it's not converted to char*. The result of the
expression &source is the address of the array, not the address of its
first element. It is (in some sense) the same value, but of a
different type; it's a pointer-to-array-of-6-char rather than a
pointer-to-char.

On many, probably most, implementations, the two pointer types have
the same representation, and passing them to sprintf() will have the
same effect. But the standard doesn't guarantee this, and the call
actually invokes undefined behavior. That's the insidious thing about
undefined behavior; it can behave exactly as you expect (until the
most inconvenient possible moment for it to fail).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Don Bruder | last post by:
A week or two ago, I asked here about porting Python to C. Got some good answers (Note 1) but now I've got another question. Actually, more a request for clarification of a topic that both the...
1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
25
by: Justin Robbs | last post by:
I am trying to write the communcations part of a Point of Sale program for the Convenience Store industry. The setup in each store will have varying numbers of registers. There could be as few...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
26
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
28
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout...
8
by: Peithon | last post by:
Hi, I'm initialising unit test structures using a macro as follows: #define UTMACRO(x) UT##x typedef struct { char * key; char * arr;
4
by: MimiMi | last post by:
I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly! I wouldn't be surprised if my problems have something to do with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.