Can someone explain a line of code for me please?

This is a discussion on Can someone explain a line of code for me please? within the C Programming forums, part of the General Programming Boards category; The following line Code: printf( (char*) &mytext[0], sizeof(mytext) -1); what I get is that I'm printing the size of mytext ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    52

    Can someone explain a line of code for me please?

    The following line

    Code:
    printf( (char*) &mytext[0], sizeof(mytext) -1);
    what I get is that I'm printing the size of mytext array - 1?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You are printing the first string contained in what appears to be an array. In that string, there is a formatting character (maybe %d or %i or %u) that is expecting the defined size of your array, less the null terminating character.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Are you kidding? Where are the printf() format specifiers? And what is the array type for mytext[]?

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    The string is something like this.
    What do you mean kidding? can't we ask questions here if we don't know the answer?

    Code:
    const char mytext[] =  " dsPIC33F Demo ";

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    That doesn't make sense. First of all, (char*)&mytext[0] can be rewritten as just mytext. Secondly, since you are passing a second parameter to printf(), the first parameter should contain a format specifier (like %d). The text " dsPIC33F Demo " doesn't have any format specifiers, so why are you passing a second parameter to printf()?
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    Given that string the result will be the same as:
    Code:
    printf(" dsPIC33F Demo ",15);
    This will just print the mytext string. The second parameter will be ignored. The conversion to char * is pointless here.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    This is how it originally was and the I added the printf.

    Code:
    void Update_LCD( void );
    const char mytext[] =  " dsPIC33F Demo ";
    const char mytext1[] = "Press S3 to cont";
    const char time_msg[] = "Time 00: 00: 00 ";
    const char adc_msg1[] =" RP5 = 0.00 Vdc  ";
    
    
    int main ( void )
    {
    
    	/* Welcome message */
    	home_clr();
    	puts_lcd( (char*) &mytext[0], sizeof(mytext) -1 );
    	line_2();
    	puts_lcd( (char*) &mytext1[0], sizeof(mytext1) -1 );
    Code:
    void puts_lcd( unsigned char *data, unsigned char count ) 
    {
      	while ( count )
    	{
    		lcd_data( *data++ );
    		count --;
    	}	
    }
    Last edited by drkidd22; 12-09-2009 at 07:21 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well "puts_lcd" is not the same thing as printf. It looks more like a fwrite style...


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question (for you not for me!)
    By antonis in forum C Programming
    Replies: 35
    Last Post: 11-10-2005, 11:41 AM
  2. Can't figure out a line of code...
    By bamera in forum C++ Programming
    Replies: 1
    Last Post: 10-15-2005, 07:11 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21