Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C char function on Visual Studio C 2019 wrong result, or not.

Status
Not open for further replies.

nagkiller

Full Member level 4
Joined
Jul 9, 2009
Messages
238
Helped
37
Reputation
74
Reaction score
37
Trophy points
1,308
Location
Brazil
Activity points
2,703
Could anyone help me with a error in char function on Visual Studio C 2019.

C++:
#include <windows.h>
#include <iostream>

char *timeStamp();

int main(void) {
    printf_s("%s", timeStamp());
    return 0;
}

char *timeStamp() {
    SYSTEMTIME t;
    GetLocalTime(&t);
    char buffer[35];
    int j;
    j = sprintf_s(buffer, 25, "%02d/", t.wDay);
    j += sprintf_s(buffer + j, 30 - j, "%02d/", t.wMonth);
    j += sprintf_s(buffer + j, 30 - j, "%04d ", t.wYear);
    j += sprintf_s(buffer + j, 30 - j, "%02d:", t.wHour);
    j += sprintf_s(buffer + j, 30 - j, "%02d:", t.wMinute);
    j += sprintf_s(buffer + j, 30 - j, "%02d = ", t.wSecond);
    printf_s("%s\n", buffer);
    return buffer;
}

Inside function, result is ok, but in main function it is printing differents characters.

Code:
Inside function ===> 10/11/2020 22:07:11 =
Outside function ==> ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠♀■´

If I change main function to use strncpy_s, a half of result is printed.

C++:
int main(void) {
    char timeNow[35];    
    strncpy_s(timeNow,35, timeStamp(), 34);
    printf_s("%s\n", timeNow);
    return 0;
}

The result is:
Code:
10/11/20☺

Thanks in advance.
 
Last edited:

The "buffer" is local to the function (on the stack), so the contents can change after you exit the function.
Make buffer "static" or provide a buffer in the function call.
 
Hi,

C char function on Visual Studio C 2019 wrong result, or not.

Altough I´m not a C expert.... (please experts correct me if I´m wrong)
I´d like to clarify.
"char" is not a function. "char" is the declarion of the variable for the return value of the function "timestamp".

The function "timestamp" defines an array, thus it transferres "timestamp" as pointer.
It´s likely that in main you expect a char (= usually a byte), but you receive a pointer (= an address of maybe 32 bit in length).

or something like that....

Klaus
 
"char" is not a function. "char" is the declarion of the variable for the return value of the function "timestamp".

The function "timestamp" defines an array, thus it transferres "timestamp" as pointer.
It´s likely that in main you expect a char (= usually a byte), but you receive a pointer (= an address of maybe 32 bit in length).

char *timeStamp(); // "char *" means that the function returns a pointer to "char", in this case a pointer to the first element of an array
"char *" is the normal "type" for a strings in C/C++. It is only a pointer to the first character in a null-terminated string.

The problem in this case is that the string pointer points to an area that isn't valid after exit from the function,
because the area "buffer" is located in the stack frame for the function.
A simple fix is to move "buffer" into a dedicated memory area by adding the keyword "static":

static char buffer[35];

The drawback with this solution is that the function isn't "reentrant", so only one thread should use it.
 
Sorry for bad english or don't express myself correctly.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top