473,804 Members | 3,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assembling a string

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.

Feb 23 '06
31 2001
"Rod Pemberton" <do*********@so rry.bitbucket.c mm> wrote:
"Vladimir S. Oka" <no****@btopenw orld.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
sprintf(all,"%f , %d, %c", a, b, c);
^


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
Feb 23 '06 #11
JAKE wrote:

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:Th***** ********@gmail. com>

Feb 23 '06 #12
JAKE wrote:
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/
Feb 23 '06 #13
Kenneth Brody wrote:
JAKE wrote:
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.

--
imalone
Feb 23 '06 #14
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.

--
pete
Feb 23 '06 #15
Richard Bos wrote:
"Rod Pemberton" <do*********@so rry.bitbucket.c mm> wrote:
"Vladimir S. Oka" <no****@btopenw orld.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
sprintf(all,"%f , %d, %c", a, b, c);
^


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.


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

Brian

Feb 23 '06 #16
"Vladimir S. Oka" <no****@btopenw orld.com> writes:
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 */


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
Feb 23 '06 #17
pete wrote:
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!?


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.c om, 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/googlegroupsrep ly/>
Feb 24 '06 #18
In article <43************ **@cam.ac.uk> Ian Malone <ib***@cam.ac.u k> wrote:
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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 24 '06 #19
Micah Cowan wrote:
"Vladimir S. Oka" <no****@btopenw orld.com> writes:

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).

--
BR, Vladimir

Sushido, n:
The way of the tuna.

Feb 24 '06 #20

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

Similar topics

1
1715
by: Matthew White | last post by:
Hi, I am assembling navigation links for an online article. The code works fine for WIN IE 6 users, but not for NN4, or Mac IE5 users. When it works, the resulting link looks like this: http://www.mydomain.com/pr3/article2.php On the machines with trouble, we get this:
16
6757
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site... this number is always changing as I have hundreds of thousand of pages of text on my site. Problem: - in my opinion this just not only look weird, but the variable "n" (number)
5
31184
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it will split myString up using the delimiter of 1 space so that
9
8006
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
9
3700
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can somebody please show me the code to do this?
10
8196
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
37
4723
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie, 20-30k), what are the performance differences (if any with something as trivial as this) between initializing a new instance of StringBuilder with a specified capacity vs. initializing a new instance without... (the final length is not fixed) ie,
2
4786
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
7
2029
by: carl.manaster | last post by:
Hi, I'd like to take a string containing MSIL code, assemble it, execute it, and receive the result all from my running C# application. So far I've managed to manually create some MSIL code that I can successfully assemble with ilasm and execute at the DOS prompt, and I've read up on System.Reflection.Emit and CodeDom, but I don't see how to do this. It looks like I would have to write a whole System.CodeDom.Compiler.CodeCompiler; I...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.