You are not logged in.

#1 2010-06-24 01:32:42

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

C parameter question

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

#2 2010-06-24 01:40:09

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,306

Re: C parameter question

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

#3 2010-06-24 01:45:23

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

Re: C parameter question

ewaller wrote:

yes ;-)

Can you demonstrate? smile

EDIT: I should phrase it this way: "Please show me how to do that." tongue

Offline

#4 2010-06-24 02:02:39

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,306

Re: C parameter question

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

#5 2010-06-24 02:33:25

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,306

Re: C parameter question

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

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

#6 2010-06-24 08:58:56

Rip-Rip
Member
Registered: 2008-11-09
Posts: 32

Re: C parameter question

Yes you can smile

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

#7 2010-06-27 00:19:56

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

Re: C parameter question

ewaller wrote:
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! smile 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

#8 2010-06-27 01:16:28

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

Re: C parameter question

Rip-Rip wrote:

Yes you can smile

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 smile

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

Board footer

Powered by FluxBB