printf %s doesn't print the string

GPC2 script programming for Titan Two. Code examples, questions, requests.

printf %s doesn't print the string

Postby titansattack » Sat Dec 16, 2017 3:43 am

How do I print the string using printf? The documentation mentions %s is the pointer address. I want to print the string.

Code: Select all
 
char* macro_up = "up.gmk";
 
printf(macro_name); // up.gmk
 
printf("macro: %s", macro_name);
 


https://www.consoletuner.com/wiki/index ... =t2:printf
User avatar
titansattack
Sergeant First Class
Sergeant First Class
 
Posts: 21
Joined: Sun Nov 13, 2016 1:53 am

Re: printf %s doesn't print the string

Postby antithesis » Sat Dec 16, 2017 9:26 pm

Have you defined "macro_name"?
Official Australian retailer for Titan One, Titan Two and XIM APEX at Mod Squad
User avatar
antithesis
Colonel
Colonel
 
Posts: 1912
Joined: Sat May 28, 2016 10:45 pm

Re: printf %s doesn't print the string

Postby J2Kbr » Mon Dec 18, 2017 9:46 am

For optimization reasons, the printf expansion is done on Gtuner IV (if doing on the device, the firmware would increase over 20K just to have the vsnprintf() required by the printf function).

The down side of this approach is that strings (as arguments of the formatting string) are evaluated to its address and not by the value.

Here is a workaround for what is required:
Code: Select all
char *MacroNames[] = {
    "macro: up.gmk"
};
 
int macro = 0;
 
init {
    printf(MacroNames[macro]);
}
 
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm

Re: printf %s doesn't print the string

Postby Scachi » Mon Dec 18, 2017 10:01 am

J2Kbr wrote:For optimization reasons, the printf expansion is done on Gtuner IV (if doing on the device, the firmware would increase over 20K just to have the vsnprintf() required by the printf function).

The down side of this approach is that strings (as arguments of the formatting string) are evaluated to its address and not by the value.

Here is a workaround for what is required:
Code: Select all
char *MacroNames[] = {
    "macro: up.gmk"
};
 
int macro = 0;
 
init {
    printf(MacroNames[macro]);
}
 


That example should be added to the docs :smile0517:
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: printf %s doesn't print the string

Postby Scachi » Thu Jun 21, 2018 10:45 am

Is there an option to output multiple strings from the *MacroNames[] in one line ? I'm lost with pointers and stuff..
That doesn't work:
Code: Select all
printf(MacroNames[0],MacroNames[0]);

or is there a printf statement switch to use it without a linebreak at the end ?
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: printf %s doesn't print the string

Postby J2Kbr » Thu Jun 21, 2018 3:59 pm

printf can accept only one string in the argument list (the format string). Please give me an idea of what you are trying to implement, maybe I can find an alternative solution.
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm

Re: printf %s doesn't print the string

Postby Scachi » Thu Jun 21, 2018 4:27 pm

It is not important but would allow more flexibility on text output to the output panel.

I did just want to output some data for debugging a script, that data is a big char *MacroNames[] with 50+ entries.
As there are multiple selections at the same time it would have been nice to put them out side by side in a printf statement.

If we had a prinf statement without a linefeed/brake at the end I could have printed them like that in a single line in the output panel:
Code: Select all
"macro: up.gmk" - "macro: down.gmk" - "macro: left.gmk" - "macro: right.gmk"

with multiple commands like
Code: Select all
 
printf(MacroNames[0]); printf(" - ");
printf(MacroNames[1]); printf(" - ");
printf(MacroNames[2]); printf(" - ");
printf(MacroNames[3]);
printf("\n");
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: printf %s doesn't print the string

Postby J2Kbr » Thu Jun 21, 2018 5:02 pm

Maybe this will work for you?
Code: Select all
char *MacroNames[] = {
    "macro: up.gmk",
    "macro: down.gmk",
    "macro: left.gmk",
    "macro: right.gmk",
};
 
init {
    print(MacroNames[0]);
    print(" - ");
    print(MacroNames[1]);
    print(" - ");
    print(MacroNames[2]);
    print(" - ");
    print(MacroNames[3]);
    print(NULL); // NULL to actual printf
}
 
void print(char *str) {
    static char buffer[256], *tail;
 
    if(str) {
        if(!tail) {
            tail = &buffer;
        }
        while(*str) {
            *tail++ = *str++;
        }
    } else if(tail) {
        printf(&buffer);
        tail = NULL;
    }
}
 
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm

Re: printf %s doesn't print the string

Postby Scachi » Thu Jun 21, 2018 5:17 pm

! awesome ! Thank you very much.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany


Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 126 guests