Assembling a string

J

JAKE

I'm pretty new to ansi c and I'm stuck I'm trying to assemble a string
in a called function. I need to send it three different data types and
return the assembled string. I've been getting errors such as...

28 C:\Dev-Cpp\assemble.c conflicting types for 'assemble'
3 C:\Dev-Cpp\assemble.c previous declaration of 'assemble' was here
30 C:\Dev-Cpp\assemble.c syntax error before "a"

here's what I have so far....

#include <stdio.h>

void assemble(float, int, char, char[]);

int main()
{
float a;
int b;
char c, all[6];

printf("ENTER A FLOATING POINT NUMBER:\n");
scanf("%f", &a);

printf("/nENTER A INTERGER:\n");
scanf("%d", &b);

printf("/nENTER A CHARACTER:\n:");
scanf("%c", &c);

assemble(a, b, c, all);

puts(all);

return 0;
}

void assemble (float *a, int *b, char *c, char *all)
{

sprintf(all,"%f, %d, %c" a, b, c);

return;
}

Am I supposed to convert the data types before I pass them to the
function?
Appreciate any help.
 
R

Richard G. Riley

I'm pretty new to ansi c and I'm stuck I'm trying to assemble a string
in a called function. I need to send it three different data types and
return the assembled string. I've been getting errors such as...

28 C:\Dev-Cpp\assemble.c conflicting types for 'assemble'
3 C:\Dev-Cpp\assemble.c previous declaration of 'assemble' was here
30 C:\Dev-Cpp\assemble.c syntax error before "a"

here's what I have so far....

#include <stdio.h>

void assemble(float, int, char, char[]);

int main()
{
float a;
int b;
char c, all[6];

printf("ENTER A FLOATING POINT NUMBER:\n");
scanf("%f", &a);

printf("/nENTER A INTERGER:\n");
scanf("%d", &b);

printf("/nENTER A CHARACTER:\n:");
scanf("%c", &c);

assemble(a, b, c, all);

puts(all);

return 0;
}

void assemble (float *a, int *b, char *c, char *all)
{

sprintf(all,"%f, %d, %c" a, b, c);

return;
}

Am I supposed to convert the data types before I pass them to the
function?
Appreciate any help.

You have declared assemble to expect POINTERS to the data. Whereas you
have passed the data by value.

Without giving the solution:

int a; // a is an integer
int * b; //b is apointer to an integer

b = &a; // assign the address of a to b;
*b=1; // put 1 into the address pointed to be b. In this case, now a
== 1
 
T

tmp123

JAKE said:
I'm pretty new to ansi c and I'm stuck I'm trying to assemble a string
in a called function. I need to send it three different data types and
return the assembled string. I've been getting errors such as...

28 C:\Dev-Cpp\assemble.c conflicting types for 'assemble'
3 C:\Dev-Cpp\assemble.c previous declaration of 'assemble' was here
30 C:\Dev-Cpp\assemble.c syntax error before "a"

here's what I have so far.... ....
void assemble(float, int, char, char[]); ....
void assemble (float *a, int *b, char *c, char *all)
....


The compiler complains that the two previous lines are not equal
declarations.
 
P

pemo

JAKE said:
I'm pretty new to ansi c and I'm stuck I'm trying to assemble a string
in a called function. I need to send it three different data types
and return the assembled string. I've been getting errors such as...

28 C:\Dev-Cpp\assemble.c conflicting types for 'assemble'
3 C:\Dev-Cpp\assemble.c previous declaration of 'assemble' was here
30 C:\Dev-Cpp\assemble.c syntax error before "a"

here's what I have so far....

#include <stdio.h>

void assemble(float, int, char, char[]);

int main()
{
float a;
int b;
char c, all[6];

printf("ENTER A FLOATING POINT NUMBER:\n");
scanf("%f", &a);

printf("/nENTER A INTERGER:\n");
scanf("%d", &b);

printf("/nENTER A CHARACTER:\n:");
scanf("%c", &c);

assemble(a, b, c, all);

puts(all);

return 0;
}

void assemble (float *a, int *b, char *c, char *all)
{

sprintf(all,"%f, %d, %c" a, b, c);

return;
}

Am I supposed to convert the data types before I pass them to the
function?
Appreciate any help.

Change

void assemble(float, int, char, char[]);

To

void assemble(float *, int *, char *, char[]);

or

void assemble(float *, int *, char *, char *);


Change

assemble(a, b, c, all);

To

assemble(&a, &b, &c, all);

Or

assemble(&a, &b, &c, &all[0]);
 
K

Keith Thompson

JAKE said:
here's what I have so far....

#include <stdio.h>

void assemble(float, int, char, char[]);

int main()
{
float a;
int b;
char c, all[6];

printf("ENTER A FLOATING POINT NUMBER:\n");
scanf("%f", &a);

printf("/nENTER A INTERGER:\n");
scanf("%d", &b);

printf("/nENTER A CHARACTER:\n:");
scanf("%c", &c);

assemble(a, b, c, all);

puts(all);

return 0;
}

void assemble (float *a, int *b, char *c, char *all)
{

sprintf(all,"%f, %d, %c" a, b, c);

return;
}

That doesn't even compile. The declaration and definition of
assemble() are inconsistent, and you have a syntax error on the
sprintf() call. Also, I assume you mean "\n" rather than "/n".

Post your actual code (cut-and-paste, don't re-type). Otherwise we
have no way of guessing which errors are in your code and which ones
you introduced when you posted an approximation of it.
 
V

Vladimir S. Oka

JAKE wrote:

said:
void assemble (float *a, int *b, char *c, char *all)
{

sprintf(all,"%f, %d, %c" a, b, c);

Apart from what others have pointed out, you're missing a comma here.
That's why you get the third error.

sprintf(all,"%f, %d, %c", a, b, c);
^
return;
}

Also, alloting only 6 bytes to `all` is sure to give you a buffer
overflow above. One way around it is to declare `all` with whatever
size you think you need, but ensure that `sprintf` can't give you a
larger string (also taking care about the terminating \0). E.g.:

char all[26];

sprintf(all, "%10f, %10d, %1c", a, b, c); /* single blanks */
 
K

Keith Thompson

Keith Thompson said:
JAKE said:
here's what I have so far....
[snip]

That doesn't even compile. The declaration and definition of
assemble() are inconsistent, and you have a syntax error on the
sprintf() call. Also, I assume you mean "\n" rather than "/n".

Post your actual code (cut-and-paste, don't re-type). Otherwise we
have no way of guessing which errors are in your code and which ones
you introduced when you posted an approximation of it.

Sorry, I didn't read the original message closely enough. I see that
you *did* post your actual code, and you were asking about the
compilation errors.

I think others have answered your questions.
 
R

Rod Pemberton

Vladimir S. Oka said:
JAKE wrote:

void assemble (float *a, int *b, char *c, char *all)
{

sprintf(all,"%f, %d, %c" a, b, c);

Apart from what others have pointed out, you're missing a comma here.
That's why you get the third error.

sprintf(all,"%f, %d, %c", a, b, c);
^
return;
}

Also, alloting only 6 bytes to `all` is sure to give you a buffer
overflow above. One way around it is to declare `all` with whatever
size you think you need, but ensure that `sprintf` can't give you a
larger string (also taking care about the terminating \0). E.g.:

char all[26];

sprintf(all, "%10f, %10d, %1c", a, b, c); /* single blanks */

Vladimir,

Are you using a fixed with font? FYI, the caret above is place under the %
of the %d in my newsreader...

RP
 
V

Vladimir S. Oka

Rod said:
Vladimir S. Oka said:
JAKE wrote:

void assemble (float *a, int *b, char *c, char *all)
{

sprintf(all,"%f, %d, %c" a, b, c);

Apart from what others have pointed out, you're missing a comma here.
That's why you get the third error.

sprintf(all,"%f, %d, %c", a, b, c);
^
return;
}

Also, alloting only 6 bytes to `all` is sure to give you a buffer
overflow above. One way around it is to declare `all` with whatever
size you think you need, but ensure that `sprintf` can't give you a
larger string (also taking care about the terminating \0). E.g.:

char all[26];

sprintf(all, "%10f, %10d, %1c", a, b, c); /* single blanks */

Vladimir,

Are you using a fixed with font? FYI, the caret above is place under the %
of the %d in my newsreader...

I'm forced to use blinkin' Google from the office. I tried my best with
Preview, but obviously failed. I now realise it'd have been better if I
just spelled it out: "missing comma after closing double quote". :-(
 
V

Vladimir S. Oka

Vladimir said:
Rod said:
Vladimir S. Oka said:
JAKE wrote:

<snip>

void assemble (float *a, int *b, char *c, char *all)
{

sprintf(all,"%f, %d, %c" a, b, c);

Apart from what others have pointed out, you're missing a comma here.
That's why you get the third error.

sprintf(all,"%f, %d, %c", a, b, c);
^


return;
}

Also, alloting only 6 bytes to `all` is sure to give you a buffer
overflow above. One way around it is to declare `all` with whatever
size you think you need, but ensure that `sprintf` can't give you a
larger string (also taking care about the terminating \0). E.g.:

char all[26];

sprintf(all, "%10f, %10d, %1c", a, b, c); /* single blanks */

Vladimir,

Are you using a fixed with font? FYI, the caret above is place under the %
of the %d in my newsreader...

I'm forced to use blinkin' Google from the office. I tried my best with
Preview, but obviously failed. I now realise it'd have been better if I
just spelled it out: "missing comma after closing double quote". :-(

However, it looks OK viewed through Googgles (I'm using fixed-width
view, which seems to be utilising Courier New typeface). Could it be
/your/ font? ;-)
 
R

Richard Bos

Rod Pemberton said:
Vladimir,

Are you using a fixed with font? FYI, the caret above is place under the %
of the %d in my newsreader...

Then I suspect it's your newsreader that has the problem; it's under the
comma in mine, which I've set to use Courier.

Richard
 
K

Kenneth Brody

JAKE said:
I'm pretty new to ansi c and I'm stuck I'm trying to assemble a string
in a called function. I need to send it three different data types and
return the assembled string. I've been getting errors such as...

28 C:\Dev-Cpp\assemble.c conflicting types for 'assemble'
3 C:\Dev-Cpp\assemble.c previous declaration of 'assemble' was here
30 C:\Dev-Cpp\assemble.c syntax error before "a"

here's what I have so far....

#include <stdio.h>

You prototype the function one way:
void assemble(float, int, char, char[]);
[...]

Then you define it another way:
void assemble (float *a, int *b, char *c, char *all) [...]
Appreciate any help.

Pick one way, and stick with it. Either you want float/int/char, or you
want pointers to float/int/char.

Note that the call in main() to this function, and the body of the function,
both imply that you want the values, not the pointers.

Also, note that you define all as a 6-character array, and then sprintf()
into it much more than 6 characters.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

Martin Ambuhl

JAKE said:
I'm pretty new to ansi c and I'm stuck I'm trying to assemble a string
in a called function. I need to send it three different data types and
return the assembled string. I've been getting errors such as...

28 C:\Dev-Cpp\assemble.c conflicting types for 'assemble'
3 C:\Dev-Cpp\assemble.c previous declaration of 'assemble' was here
30 C:\Dev-Cpp\assemble.c syntax error before "a"

here's what I have so far....
[Prototype]
void assemble(float, int, char, char[]);

[from function definition]
void assemble (float *a, int *b, char *c, char *all)

It is obvious that 'float' is not the same as 'float *', 'int' is not
the same as 'int *', and 'char' is not the same as 'char *'.
Am I supposed to convert the data types before I pass them to the
function?

No, but
a) the function prototype must match the function definition
b) a function expecting addresses of its parameters should be supplied
with addresses of its parameters, not with their values/
 
I

Ian Malone

Kenneth said:
JAKE said:
I'm pretty new to ansi c and I'm stuck I'm trying to assemble a string
in a called function. I need to send it three different data types and
return the assembled string. I've been getting errors such as...
here's what I have so far....


You prototype the function one way:
void assemble(float, int, char, char[]);
[...]

Then you define it another way:
void assemble (float *a, int *b, char *c, char *all) [...]
Appreciate any help.

Final point to the OP in this regard: it seems to be pretty common
that newcomers to C use function prototypes and put their functions
in the order: main, function1, function2 ...

It's more common practice to put the functions in reverse order
(with main at the bottom if it's present, small helpers at the
top). Mainly for this reason; you eliminate the need for the
separate prototype. If you do need a separate prototype (for a
header, because of interdependent functions or because you'll
need to interleave unrelated functions otherwise), then cut and
paste it.
 
P

pete

Ian Malone wrote:
Final point to the OP in this regard: it seems to be pretty common
that newcomers to C use function prototypes and put their functions
in the order: main, function1, function2 ...

It's more common practice to put the functions in reverse order
(with main at the bottom if it's present, small helpers at the
top). Mainly for this reason; you eliminate the need for the
separate prototype.

Saving Keystrokes!?
If you do need a separate prototype (for a
header, because of interdependent functions or because you'll
need to interleave unrelated functions otherwise), then cut and
paste it.

I think it's better to write and use prototypes by default,
and completely dispense with any need to
even consider the order the definitions.
 
D

Default User

Richard said:
Then I suspect it's your newsreader that has the problem; it's under
the comma in mine, which I've set to use Courier.

Moi aussi. "Courier New" to be specific. Looked fine.



Brian
 
M

Micah Cowan

Vladimir S. Oka said:
JAKE wrote:

void assemble (float *a, int *b, char *c, char *all)
{

sprintf(all,"%f, %d, %c" a, b, c);

Apart from what others have pointed out, you're missing a comma here.
That's why you get the third error.

sprintf(all,"%f, %d, %c", a, b, c);
^
return;
}

Also, alloting only 6 bytes to `all` is sure to give you a buffer
overflow above. One way around it is to declare `all` with whatever
size you think you need, but ensure that `sprintf` can't give you a
larger string (also taking care about the terminating \0). E.g.:

char all[26];

sprintf(all, "%10f, %10d, %1c", a, b, c); /* single blanks */

I'm sorry to say that the above format string does not do what you
think it does. For most of the conversion specifiers, there is no way
to specify a /maximum/ number of characters to use for the field: what
you have done is to specify the /minimum/ number of characters to
appera in the field.

There's really no substitute for using snprintf() in cases like
these. You've got to pass in the size of the buffer along with a
pointer into the buffer itself.

-Micah
 
C

CBFalconer

pete said:
Saving Keystrokes!?

No. Avoiding errors. Any time you have to maintain two copies to
be identical, it is easy to foul up. It also has the advantage
that you know immediately which way to look for the function
source. The only times you need a prototype is for external access
and for mutual recursion.

--
"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/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

Chris Torek

It's more common practice to put the functions in reverse order
(with main at the bottom if it's present, small helpers at the
top). Mainly for this reason; you eliminate the need for the
separate prototype.

I do not know about "more common", but it does indeed eliminate the
need for most prototypes. It is also the kind of system-building
that is often referred-to as "bottom-up": you write the bottom-level
supporting functions first, and read and comprehend them. Then
you use those to assemble medium-level constructs; you use those
to assemble the complete program.

There are adherents of the "top-down" style, in which you define the
overall problem first, and begin by breaking it down into major
components. Then you write those major components, assuming that
any complicated parts they need to perform are also already written,
and simply calling those (not yet written) functions. Then you
work on those, breaking them down as needed.

I use neither approach. :)

(Aside: I once saw the "top-down programming" approach illustrated
as a quite funny bit of artwork. There was a river [or other
physical obstacle] over which a bridge was being built ... from
the top down. The partially-complete bridge was magically floating
above the river.)

The problem with top-down programming is that you *assume* that
some function f() will take care of some (complete) sub-problem.
You build a whole lot of code based on that assumption ... then,
later, you go to write f() and discover that it is an absolutely
terrible way to solve the problem, that it depends on a solution
that was going to be computed later in function h(), which you
earlier assumed would not need to be done until f() and g() had
both completed.

The problem with bottom-up programming is that you *assume* that
some function f() is going to be useful later. By the time you
get around to writing the code that needs f(), you discover that
f() does too little and too much, just as with the top-down
method.

I tend to use what has sometimes been described as "outside-in"
programming, where I attack all the problems I understand from both
the bottom and top ends, and leave the problems I do not quite
understand to be handled by the solutions to the ones that I do.
 
V

Vladimir S. Oka

Micah said:
Vladimir S. Oka said:
char all[26];

sprintf(all, "%10f, %10d, %1c", a, b, c); /* single blanks */

I'm sorry to say that the above format string does not do what you
think it does. For most of the conversion specifiers, there is no way
to specify a /maximum/ number of characters to use for the field: what
you have done is to specify the /minimum/ number of characters to
appera in the field.

Ah, yes. I stand corrected. I blame my upbringing^H^H^H^H^H^H^H^H^H^H
past five years in which *printf() was banned, even for debug output
and logging (deeply embedded, hard real time system). Only a simple,
home brewed, print_str(char *) is available. ;-)
There's really no substitute for using snprintf() in cases like
these. You've got to pass in the size of the buffer along with a
pointer into the buffer itself.

You're quite right again. It has crossed my mind, but I decided against
it, as OP may not have access to a fully conforming C99 implementation
(they tend to use antique Borland compilers in some beginner C courses,
it seems, at least judging by some recent posts here).
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top