473,387 Members | 1,476 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,387 software developers and data experts.

Doubt in printing results on function!

Hi there!

I'm creating a function that copies some information to a buffer and
enters a delimiter string "//" between results.

My issue here is when i print the buffer it appears this weird result:

cod : 2
id : 20
Buffer is 
Buffer is // 
final buffer is: // 

What am i doing wrong for this to happen? I'm typing bellow the
function used. Thanks in advance for any help.
int criaRespCreate(int cod, int id, char *buffer){
printf("\n cod : %d", cod);
printf("\n id : %d", id);

int tam = 0;
char str[]=" // ";
int tcd = strlen(str);

// insert ID

memcpy(buffer,&id,LENGTH);

printf("\n Buffer is %s", buffer);

// Insert delimitador caracter

memcpy(buffer + tam,&str,tcd);
tam += tcd;

// inserir code

memcpy(buffer + tam,&cod,LENGTH);
tam += LENGTH;

// Insert delimitador caracter

memcpy(buffer + tam ,&str,tcd);
tam += tcd;

printf("\n Buffer is %s", buffer);

return tam;
}

Dec 2 '06 #1
10 1405
Never mind! I've solved the issue.

For anyone who wishes to know the issue was that i can't print directly
the buffer to the screen using a simple printf function. I have to
create an auxiliary function that copies the content of a specific
offset to a variable and then print it!

Off to work then! =)

Dec 2 '06 #2
Pedro Pinto wrote:
>
Never mind! I've solved the issue.

For anyone who wishes to know the issue was
that i can't print directly
the buffer to the screen using a simple printf function. I have to
create an auxiliary function that copies the content of a specific
offset to a variable and then print it!

Off to work then! =)
/* BEGIN new.c */

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

#define DELIM " // "

int criaRespCreate(int cod, int id, char *buffer)
{
int tam = sprintf(buffer, "%d%s%d%s", id, DELIM, cod, DELIM);

printf("\n cod : %d\n", cod);
printf( " id : %d\n", id);
printf("\nBuffer is :%s\n", buffer);
return tam;
}

int main(void)
{
int cod = 2;
int id = 20;
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];

printf("tam is %d\n", criaRespCreate(cod, id, buffer));
return 0;
}

/* END new.c */

--
pete
Dec 2 '06 #3
MQ

Pedro Pinto wrote:
int criaRespCreate(int cod, int id, char *buffer){
printf("\n cod : %d", cod);
printf("\n id : %d", id);

int tam = 0;
char str[]=" // ";
int tcd = strlen(str);

// insert ID

memcpy(buffer,&id,LENGTH);

printf("\n Buffer is %s", buffer);

// Insert delimitador caracter

memcpy(buffer + tam,&str,tcd);
tam += tcd;

// inserir code
memcpy(buffer + tam,&cod,LENGTH);
tam += LENGTH;
You can't just convert cod to a string by copying it into a char
buffer, and that looks like what you are trying to do. And what is
LENGTH, you have not provided us the definition. This will produce
totally undefined behaviour depending on how the integer data type is
represented.

MQ

Dec 2 '06 #4
Pedro Pinto wrote:
Hi there!

I'm creating a function that copies some information to a buffer and
enters a delimiter string "//" between results.

My issue here is when i print the buffer it appears this weird result:

cod : 2
id : 20
Buffer is 
Buffer is // 
final buffer is: // 
You haven't told us what your intended result is.
What am i doing wrong for this to happen? I'm typing bellow the
function used. Thanks in advance for any help.
int criaRespCreate(int cod, int id, char *buffer){
printf("\n cod : %d", cod);
printf("\n id : %d", id);

int tam = 0;
char str[]=" // ";
int tcd = strlen(str);
size_t would be better.
>
// insert ID

memcpy(buffer,&id,LENGTH);
What is LENGTH? What did you want this statement to do?

At the moment you're just copying the bit pattern for an int
directly into a buffer that appears to be character string.
The resultant 'characters' needn't be printable.
printf("\n Buffer is %s", buffer);

// Insert delimitador caracter

memcpy(buffer + tam,&str,tcd);
tam += tcd;

// inserir code

memcpy(buffer + tam,&cod,LENGTH);
tam += LENGTH;

// Insert delimitador caracter

memcpy(buffer + tam ,&str,tcd);
tam += tcd;

printf("\n Buffer is %s", buffer);

return tam;
}
It looks like you want something closer to...

int criaRespCreate(int cod, int id, char *buffer)
{
return sprintf(buffer, "%d // %d // ", id, cod);
}

--
Peter

Dec 2 '06 #5
On Sat, 02 Dec 2006 02:01:48 GMT, pete <pf*****@mindspring.comwrote:
>Pedro Pinto wrote:
>>
Never mind! I've solved the issue.

For anyone who wishes to know the issue was
that i can't print directly
the buffer to the screen using a simple printf function. I have to
create an auxiliary function that copies the content of a specific
offset to a variable and then print it!

Off to work then! =)

/* BEGIN new.c */

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

#define DELIM " // "

int criaRespCreate(int cod, int id, char *buffer)
{
int tam = sprintf(buffer, "%d%s%d%s", id, DELIM, cod, DELIM);

printf("\n cod : %d\n", cod);
printf( " id : %d\n", id);
printf("\nBuffer is :%s\n", buffer);
return tam;
}

int main(void)
{
int cod = 2;
int id = 20;
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];

printf("tam is %d\n", criaRespCreate(cod, id, buffer));
return 0;
}

/* END new.c */
sizeof DELIM equals sizeof(char*). That would most likely differ from
strlen(DELIM), which is what I think you meant.

--
jay
Dec 2 '06 #6
>On Sat, 02 Dec 2006 02:01:48 GMT, pete <pf*****@mindspring.comwrote:
[enormous amounts of snippage]
>>#define DELIM " // "
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];
In article <g9********************************@4ax.com>,
jaysome <ja*****@spamcop.netwrote:
>sizeof DELIM equals sizeof(char*).
Possibly, but generally not. Since DELIM expands to " // ", sizeof
DELIM must be exactly 5. The reason is that " // " is an array
containing 5 "char"s -- specifically {' ', '/', '/', ' ', '\0'} --
so its size must be 5 * sizeof(char), or 5 * 1, or 5. This is
the same as sizeof(char *) only if sizeof(char *) is 5 (which is
unusual, in my experience).
>That would most likely differ from strlen(DELIM) ...
This is true, because strlen() does not count the terminating '\0'.
>which is what I think you meant.
No, had he used strlen(), he would have needed an extra +1 -- and
the declaration would have been valid only in C99, because strlen(arg)
is not a constant-expression, turning "buffer" into a Variable
Length Array.

Note also that sizeof "abc\0def" is 8, while strlen("abc\0def") is
3: the result of sizeof will be more than one greater than the
equivalent strlen if the string literal contains embedded '\0'
characters.
--
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.
Dec 2 '06 #7
jaysome wrote:
>
On Sat, 02 Dec 2006 02:01:48 GMT, pete <pf*****@mindspring.comwrote:
Pedro Pinto wrote:
>
Never mind! I've solved the issue.

For anyone who wishes to know the issue was
that i can't print directly
the buffer to the screen using a simple printf function. I have to
create an auxiliary function that copies the content of a specific
offset to a variable and then print it!

Off to work then! =)
/* BEGIN new.c */

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

#define DELIM " // "

int criaRespCreate(int cod, int id, char *buffer)
{
int tam = sprintf(buffer, "%d%s%d%s", id, DELIM, cod, DELIM);

printf("\n cod : %d\n", cod);
printf( " id : %d\n", id);
printf("\nBuffer is :%s\n", buffer);
return tam;
}

int main(void)
{
int cod = 2;
int id = 20;
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];

printf("tam is %d\n", criaRespCreate(cod, id, buffer));
return 0;
}

/* END new.c */

sizeof DELIM equals sizeof(char*). That would most likely differ from
strlen(DELIM), which is what I think you meant.
The type of (" // ") is (array of 5 char).
String literals are not converted to pointers
when they are the operand of sizeof.

String literals are also not converted to pointers when
they are the operand of the address operator.
(&" // ")

String literals are also not converted to pointers when
they are the initializer for an array.
char array[] = " // ";

And those are the three exceptions to string literals
always being converted to pointers.

--
pete
Dec 2 '06 #8
Geeeeee i was thinking in creating a print function to extract the
results but the solutions provided here give me a lot more choices! =)

This is a client program that send with an UDP socket a buffer with
some information to a server! LENGTH is the length of an int, that is
4. Thank you for the help, i'll post later today the final solution!

Regards

Pedro Pinto

pete escreveu:
jaysome wrote:

On Sat, 02 Dec 2006 02:01:48 GMT, pete <pf*****@mindspring.comwrote:
>Pedro Pinto wrote:
>>
>Never mind! I've solved the issue.
>>
>For anyone who wishes to know the issue was
>that i can't print directly
>the buffer to the screen using a simple printf function. I have to
>create an auxiliary function that copies the content of a specific
>offset to a variable and then print it!
>>
>Off to work then! =)
>
>/* BEGIN new.c */
>
>#include <stdio.h>
>#include <string.h>
>#include <limits.h>
>
>#define DELIM " // "
>
>int criaRespCreate(int cod, int id, char *buffer)
>{
int tam = sprintf(buffer, "%d%s%d%s", id, DELIM, cod, DELIM);
>
printf("\n cod : %d\n", cod);
printf( " id : %d\n", id);
printf("\nBuffer is :%s\n", buffer);
return tam;
>}
>
>int main(void)
>{
int cod = 2;
int id = 20;
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];
>
printf("tam is %d\n", criaRespCreate(cod, id, buffer));
return 0;
>}
>
>/* END new.c */
sizeof DELIM equals sizeof(char*). That would most likely differ from
strlen(DELIM), which is what I think you meant.

The type of (" // ") is (array of 5 char).
String literals are not converted to pointers
when they are the operand of sizeof.

String literals are also not converted to pointers when
they are the operand of the address operator.
(&" // ")

String literals are also not converted to pointers when
they are the initializer for an array.
char array[] = " // ";

And those are the three exceptions to string literals
always being converted to pointers.

--
pete
Dec 2 '06 #9
Pedro Pinto wrote:
>
Geeeeee i was thinking in creating a print function to extract the
results but the solutions provided here give me a lot more choices! =)

This is a client program that send with an UDP socket a buffer with
some information to a server! LENGTH is the length of an int, that is
4. Thank you for the help, i'll post later today the final solution!
int tam = sprintf(buffer, "%d%s%d%s", id, DELIM, cod, DELIM);
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];
That should be
char buffer[2 * (sizeof id * CHAR_BIT / 3 + sizeof DELIM)];
instead.

I forgot that DELIM was written twice.

--
pete
Dec 2 '06 #10
Pedro Pinto wrote:
>
Geeeeee i was thinking in creating a print function to extract the
results but the solutions provided here give me a lot more choices! =)

This is a client program that send with an UDP socket a buffer with
some information to a server! LENGTH is the length of an int, that is
4. Thank you for the help, i'll post later today the final solution!
Please don't top-post. See the following links.

--
Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
Dec 2 '06 #11

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

Similar topics

9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
6
by: Rex Eastbourne | last post by:
Hi all, I've written the following simple macro called debug(aname, avalue) that prints out the name of an expression and its value: def debug(aname, avalue): print aname, 'is':...
4
by: Suzanka | last post by:
Hello, I have an application written in C# on visual studio .NET. It is a web aplication. The application consists of many different forms, that users occassionaly want to print out for filing....
2
by: Tim | last post by:
Hi, I have created some code for printing a column report. The default page size is 850 x 1100, which is correct. The print preview looks perfect. The data is positioned exactly where it should...
6
by: MJ | last post by:
Is it possible to print varying numbers of labels from Access?
11
by: krishnamaddi | last post by:
Hi Friends, I am having some doubt. I want to print an ASP Page, for this i need to use java script. the problem is while printing if the line is too big, i can't print those data properly . i am...
7
by: Burhan | last post by:
Hello Group: I am in the planning stages of an application that will be accessed over the web, and one of the ideas is to print a barcode that is generated when the user creates a record. The...
1
by: benfly08 | last post by:
Hi, I got a printing problem for my C# program. I need to pull out records from database and format them and then print them out. I used PrintDocument class and PrintPageEventHandler function...
8
by: Frank Rizzo | last post by:
I am trying to print huge images (much bigger than target paper). I try and use e.PageSettings.HardMarginX and e.PageSettings.HardMarginY in the PrintDocument's PrintPage event to try and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.