Doubt in printing results on function!

P

Pedro Pinto

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;
}
 
P

Pedro Pinto

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! =)
 
P

pete

Pedro said:
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 */
 
M

MQ

Pedro said:
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
 
P

Peter Nilsson

Pedro said:
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);
}
 
J

jaysome

Pedro said:
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.
 
C

Chris Torek

#define DELIM " // "
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];

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

pete

jaysome said:
Pedro said:
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.
 
P

Pedro Pinto

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 said:
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.
 
P

pete

Pedro said:
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.
 
C

CBFalconer

Pedro said:
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:
< <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/>
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top