You are not logged in.
Pages: 1
Ok, so I am currently in the process in writing a C library for a small kernel I am making for fun, and my question is:
Is it possible to have a function specify only one parameter in the code and have any extra parameters put into an array, let's call it extra[].
Here is the code I have:
unsigned int k_printf(char *message)
{
char *vidmem = (char *) 0xb8000;
unsigned int i = 0;
i = (line*80*2);
while(*message != 0) {
if(*message=='\n') {
line++;
i = (line*80*2);
*message++;
}
else if(*message=='%s') {
}
else {
vidmem[i] = *message;
*message++;
i++;
vidmemcount++;
vidmem[i] = WHITE_TXT;
i++;
vidmemcount++;
}
}
line++;
return 1;
};
I am currently trying to make a close but less powerful printf() equivalent. For example:
printf("My name is %s and my friends name is %s", myname, friendsname);
I want to be able to make an extra[] array that stores myname and friendsname in extra[0] and extra[1]. Is this possible?
Offline
yes ;-)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
yes ;-)
Can you demonstrate?
EDIT: I should phrase it this way: "Please show me how to do that."
Offline
Okay, I was just yanking your chain -- I was also looking up the reference in K&R. Check out K&R Para 4.3.
In general, pass in an array either by the name of the array:
myfunction(myArray);
which is the same as passing in the address of the first element of the array:
myFunction(&myArray[0[);
Note that you can pass in a reference to an element of the array not at the beginning using:
myFuncion(&myArray[myIndex]);
Inside the function, you can treat the reference as an array, or you can use pointer arithmetic to find the elements.
Also, You can pass in a variable number of parameter if you define your function like this:
int average( int first, ... ){insert code here}
You find the parameters using pointer arithmetic from 'first'. It is up to you to keep track of the size of things you passed in. Be very careful. printf figures this out based upon the % flags in the control string
Last edited by ewaller (2010-06-24 02:03:20)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Ok, so I am currently in the process in writing a C library for a small kernel I am making for fun, and my question is:
Is it possible to have a function specify only one parameter in the code and have any extra parameters put into an array, let's call it extra[].
Here is the code I have:
unsigned int k_printf(char *message) { char *vidmem = (char *) 0xb8000; unsigned int i = 0; i = (line*80*2); while(*message != 0) { if(*message=='\n') { line++; i = (line*80*2); *message++; } else if(*message=='%s') { } else { vidmem[i] = *message; *message++; i++; vidmemcount++; vidmem[i] = WHITE_TXT; i++; vidmemcount++; } } line++; return 1; };
I am currently trying to make a close but less powerful printf() equivalent. For example:
printf("My name is %s and my friends name is %s", myname, friendsname);
I want to be able to make an extra[] array that stores myname and friendsname in extra[0] and extra[1]. Is this possible?
Okay, here goes:
where is line defined, allocated, initialized?
where is vidmemcount defined, allocated, initialized? what is it used for?
Avoid *80*2. Especially in multiple places. Use a #define in case you change the size latter.
Even better, how about creating records on the heap using malloc and then keeping an array of references to the records.[Edit :: Never mind -- I see what you are up to. Ignore this]
Rather than :
while(*message != 0) {
if(*message=='\n') {
line++;
i = (line*80*2);
*message++;
}
else if(*message=='%s') {
}
else {
vidmem[i] = *message;
*message++;
i++;
vidmemcount++;
vidmem[i] = WHITE_TXT;
i++;
vidmemcount++;
}
How about :
char* vm = vidmem;
while ( *message)
{
if ((*message)=='\n')
{
vm=vidmem+SPAN * (++line);
message++;
}
else
{
*(vm++)=*(message++);
*(vm++)=WHITE_TXT;
}
}
edit : fixed bug in my code
Last edited by ewaller (2010-06-24 02:42:34)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Yes you can
unsigned int k_printf(char *message, ...);
Take a look at stdarg(3) if you want to know how to use the extra parameters. Don't forget that since your doing kernel developpement, you'll need to recode every stdarg function.
Offline
itsbrad212 wrote:Ok, so I am currently in the process in writing a C library for a small kernel I am making for fun, and my question is:
Is it possible to have a function specify only one parameter in the code and have any extra parameters put into an array, let's call it extra[].
Here is the code I have:
unsigned int k_printf(char *message) { char *vidmem = (char *) 0xb8000; unsigned int i = 0; i = (line*80*2); while(*message != 0) { if(*message=='\n') { line++; i = (line*80*2); *message++; } else if(*message=='%s') { } else { vidmem[i] = *message; *message++; i++; vidmemcount++; vidmem[i] = WHITE_TXT; i++; vidmemcount++; } } line++; return 1; };
I am currently trying to make a close but less powerful printf() equivalent. For example:
printf("My name is %s and my friends name is %s", myname, friendsname);
I want to be able to make an extra[] array that stores myname and friendsname in extra[0] and extra[1]. Is this possible?
Okay, here goes:
where is line defined, allocated, initialized?
where is vidmemcount defined, allocated, initialized? what is it used for?Avoid *80*2. Especially in multiple places. Use a #define in case you change the size latter.
Even better, how about creating records on the heap using malloc and then keeping an array of references to the records.[Edit :: Never mind -- I see what you are up to. Ignore this]Rather than :
while(*message != 0) { if(*message=='\n') { line++; i = (line*80*2); *message++; } else if(*message=='%s') { } else { vidmem[i] = *message; *message++; i++; vidmemcount++; vidmem[i] = WHITE_TXT; i++; vidmemcount++; }
How about :
char* vm = vidmem; while ( *message) { if ((*message)=='\n') { vm=vidmem+SPAN * (++line); message++; } else { *(vm++)=*(message++); *(vm++)=WHITE_TXT; } }
edit : fixed bug in my code
Thanks for the tips! Any speed improvement whatsoever is crucial when doing things like this.
Here are those defined at the beginning of the functions.h file:
unsigned int line = 0;
char *vidmem = (char *) 0xb8000;
unsigned int vidmemcount = 0;
Offline
Yes you can
unsigned int k_printf(char *message, ...);
Take a look at stdarg(3) if you want to know how to use the extra parameters. Don't forget that since your doing kernel developpement, you'll need to recode every stdarg function.
Thanks for the help
I took a look at stdarg.h......and WOW! *cracks knuckles* I may have to try and use GCC's stdarg.h for the time being.
Offline
Pages: 1