printf %s doesn't print the string
9 posts
• Page 1 of 1
printf %s doesn't print the string
How do I print the string using printf? The documentation mentions %s is the pointer address. I want to print the string.
https://www.consoletuner.com/wiki/index ... =t2:printf
- 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
-
titansattack - Sergeant First Class
- Posts: 21
- Joined: Sun Nov 13, 2016 1:53 am
Re: printf %s doesn't print the string
Have you defined "macro_name"?
-
antithesis - Colonel
- Posts: 1912
- Joined: Sat May 28, 2016 10:45 pm
Re: printf %s doesn't print the string
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:
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
-
J2Kbr - General of the Army
- Posts: 20323
- Joined: Tue Mar 18, 2014 1:39 pm
Re: printf %s doesn't print the string
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
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
Re: printf %s doesn't print the string
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:
or is there a printf statement switch to use it without a linebreak at the end ?
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 ?
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
Re: printf %s doesn't print the string
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
-
J2Kbr - General of the Army
- Posts: 20323
- Joined: Tue Mar 18, 2014 1:39 pm
Re: printf %s doesn't print the string
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:
with multiple commands like
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");
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
Re: printf %s doesn't print the string
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
-
J2Kbr - General of the Army
- Posts: 20323
- Joined: Tue Mar 18, 2014 1:39 pm
Re: printf %s doesn't print the string
! awesome ! Thank you very much.
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
9 posts
• Page 1 of 1
Return to GPC2 Script Programming
Who is online
Users browsing this forum: No registered users and 126 guests