lang c "clearing"/printing char*

Mar 2, 2019 at 2:30pm
Good afternoon, I have a little problem with printing char* combined with short. I have wrong output. Is there any way to fix it ?

1
2
3
4
5
6
7
8
9
char* t(unsigned short menu_number)
char * ret = "Folder num ";
short x = strlen(ret);
char *n = (int *)malloc(sizeof(char) * x + sizeof(short));
strcpy(n, ret);
char *inn = (int *)malloc(sizeof(short));
memcpy(inn, (char*)&menu_number, sizeof(unsigned short));
strcat(n, inn);
return n;


Example output of printf("%s", t(2):
Folder num ?

Debug:
n | "Folder num \x2"
Last edited on Mar 2, 2019 at 2:54pm
Mar 2, 2019 at 4:07pm
printf("%s", t(2):

Looks like t(2) is a function call. A function that returns a char*.

\x2 indicates that the character being returned on the end of "Folder num " is the non-printing character known as STX, which has numerical value 2.

That function is returning a non-printing character. This makes no sense. There is something wrong with the function t().

If I had to guess, it looks like you're trying to create a string like this: "Folder num x" where x is a number. Such that the function t(2) is meant to return "Folder num 2" ?

If so, you've completely misunderstood what a char is. Here, look at the ascii char table: http://www.asciitable.com/

The char '2' is represented by the number 50. You have simply added the number 2 to the end of a string and in a string, the number 2 represents the non-printing character STX.



Last edited on Mar 2, 2019 at 4:14pm
Mar 2, 2019 at 9:00pm
I am as confused as Repeater is here. If his suggestions don’t get you anywhere, could you post a working example to demonstrate to us the error?
Mar 4, 2019 at 2:28pm
Yeah, your code is really confusing.

Is that supposed to be the definition of the function t()? If so, you are forgetting the curly braces after lines 1 and 9.

Why are a you casting the void* returned by malloc to an int* before assigning it to a char* in lines 4 and 6?

And, as @Repeater says, line 7 isn't going to do what you want it to. You might want to look into sprintf or snprintf. I think one of these functions will do everything you need except calculate the size of the buffer that you need.

By the way, sizeof() gives you the size of the data type in bytes of memory, not the number of characters required to represent it in a string. sizeof(unsigned short) might be 2, but the string "125" is a valid unsigned short value that takes 3 characters to display in a string.
Mar 6, 2019 at 12:49pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* t(unsigned short menu_number)
{
    char * ret = "Folder num "; 
    short x = strlen(ret); 
/*    
    char *n = (int *)malloc(sizeof(char) * x + sizeof(short) ); // size = 11 + 2 = 13, 
                                                                // but error convert (int *) to (char *)
    strcpy(n, ret); 
    char *inn = (int *)malloc(sizeof(short));                   // size = 2 * 4 = 8 bytes , 
                                                                // but error convert (int *) to (char *)
    memcpy(inn, (char*)&menu_number, sizeof(unsigned short));   // inn: high_byte?, low_byte?,?,?,?,?,?,?  
                                                                // bad idea
    strcat(n, inn);                                             // 11+8 > 13 overflow?
*/    
    // Changed !
    char *n = (char *)malloc(sizeof(char) * (x + 7));   // unsigned short has max. 6 digit, 
                                                        //+1 for '\0'
    sprintf(n, "%s%d", ret, menu_number); // simple way to convert a number to string
    
    return n;
}

int main(void)
{
    printf("%s\n", t(2)); 
    printf("%s\n", t(65535)); 
    
    return 0;
}
Mar 6, 2019 at 1:40pm
> unsigned short has max. 6 digit,
It has a 'minimum' maximum of 6 digits, if you assume a short has exactly 16 bits.

But the standards only require short to have a minimum of 16 bits, not exactly 16 bits.

https://stackoverflow.com/questions/2098149/what-platforms-have-something-other-than-8-bit-char
Topic archived. No new replies allowed.