Answer the question
In order to leave comments, you need to log in
How are the arguments of a function with a variable number of parameters arranged in memory?
I am writing an OS (important, it will not work to use stdarg), there was a need for a function with a variable number of parameters. Such a function (summing n numbers):
int test(int n, ...)
{
int result = 0;
for (int* ptr = &n; n > 0; n--)
{
ptr++;
result += *ptr;
}
return result;
}
void test(char* format, ...)
{
char* ptr = format;
for (int i = -10; i < 10; i++)
{
putc(*(ptr+i));
}
}
Answer the question
In order to leave comments, you need to log in
I am writing the OS (important, it will not work to use stdarg)
And if you call such a function (with arguments "test", 'q'):
void test(char* format, ...) { char* ptr = format; for (int i = -10; i < 10; i++) { putc(*(ptr+i)); } }
The expectations were such that at least somewhere the letter q should have appeared, which did not happen.
void test(char* format, ...)
{
char *ptr = (char *)&format;
for (int i = -10; i < 10; i++)
{
putc(*(ptr+i));
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question