Problem with pointers and sprintf

This is a discussion on Problem with pointers and sprintf within the C Programming forums, part of the General Programming Boards category; I realize this is a problem with how I am sending the arguments, but I can't quite figure out how ...

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    15

    Exclamation Problem with pointers and sprintf

    I realize this is a problem with how I am sending the arguments, but I can't quite figure out how to get rid of these warnings. I have simplified it and only showed one funcion of my code.
    Code:
     
    /* In main */
    char *var = "Some text";
    box( 1, 2, var);
    
    void box ( int height, int width, char *str ){
            char string [height] [width + 20];
            sprintf( string, "%s", str );
            printf( "%s", string );
    }
    Here are my warnings
    Code:
    Progtam.c: In function 'box':
    Program.c:27: warning: passing argument 1 of 'sprintf' from incompatible pointer type
    /usr/include/stdio.h:265: note: expected 'char *' but argument is of type 'char (*)[(unsigned int)(width + 20)]'
    Program.c:28: warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[(unsigned int)(width + 20)]'
    Thank you!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,480
    The %s modifiers for both scanf and printf require a pointer to char, i.e. a char *, but you are not providing that. You have declared string to be a 2-d array of char, meaning you have an array of strings, or equivalently an array of char arrays. Using string all by itself yields a pointer to the first element, i.e &string[0], which is of type "pointer to array of width+20 chars", or as the compiler reports it 'char (*)[(unsigned int)(width + 20)]'. That is not the same as a "pointer to char" or char *. You need to point to the line in question, so you need something like [I]string.

    I'm guessing you're trying to create some sort of text-wrapping function, and relying on each row of string being adjacent in memory -- this is not a good idea. Use memcpy or for loops to copy the chars into the proper spots in the 2-d array width chars at a time, or whatever your limit is.

    Hope that clears it up.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    15
    Ok... I'm not familiar with memcpy ()...And what your saying I need to do is make [I]string? What do you mean? Sorry. And you are right that is what I am doing. The reason for the plus 20 is I am expecting color chars \033 [...

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,480
    memcpy is a standard function in the C language for copying chunks of memory. You can find tutorials all over the web for using it. Make sure to #include <string.h> if you use it.

    It's not [I]string. It's string[i] (i or I are both acceptable, but lower case i is more typical). It's how you access arrays. You don't seem to understand arrays. Again, there are tutorials all over the web. Study them then see if you can understand and fix your problem.

    As for the +20, are you sure that is enough for all your color chars? If you have small boxes like 5 chars wide, it almost certainly is. For wider boxes, like 50 or 100 however, there may be more color sequences. You may want to do some calculations to find out how many escape sequences you may actually have per line, and how long each escape will be. Also, you may need to make sure you leave room for the null (end-of-string) character.

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    15
    I do understand arrays. But I do not understand pointers very well. As for the 20 it was a random guess. But My color sequences will be limited to (per string) one for the left border, one for the main text,one for the right border plus a escape sequence to move it down one row and to the left.
    I will look up memcopy (). Thanks!

  6. #6
    Registered User
    Join Date
    Aug 2014
    Posts
    15
    I guess another option is to pint each char as I get it.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,480
    Note, it's memcpy without the 'o'.

    By "escape sequences" I should have been more clear: I meant terminal escape sequences, i.e. the groups of characters that do the color changing. Usually there are 2, 3 or more chars that are required to tell the terminal what color text/background you are using. 20 chars is probably fine for this.

    Honestly, I would not say that you understand arrays -- at least not very well. However, if it's pointers you're really struggling with, I would recommend then that you find some good tutorials online and in books, and work through them. Start with simpler examples/problems until you really have a handle on it. Pointers, while confusing at first, are one of those topics you simply can't get around knowing if you wish to program C. And the sooner you learn, the better.

    I don't know exactly what your project is, but perhaps you could look into using a library like curses (ncurses, pdcurses, etc) that handles box drawing, terminal colors, scrolling and wrapping for you automatically. It will save you a great deal of trouble, unless your goal is to learn how to write such a program yourself.

    If you wish to do this all yourself, you should really consider a better design
    Code:
    // foreground colors
    enum TerminalColorsFG {
        FG_BLUE,
        FG_GREEN,
        ...
    };
    // similar for TerminalColorsBG -- background colors
    
    struct Box {
        enum TerminalColorsFG border_fg;
        enum TerminalColorsBG border_bg;
        enum TerminalColorsFG text_fg;
        enum TerminalColorsBG text_bg;
        int x;  // horizontal position of upper-left corner
        int y;  // vertical position of upper-left corner
        int width;
        int height;
    };
    
    void DrawBox(struct Box *box, const char *text) {
        DrawBoxTop(box);
        // logic to print text wrapped at box->width chars...maybe a for loop or two
        DrawBoxBottom(box);  // will be very similar to DrawBoxTop
    }
    
    void DrawBoxTop(box) {
        GotoXY(box->x, box->y);
        SetTerminalColor(box->border_fg, box->border_bg);
        print upper-left corner char
        print some number of top border chars
        print upper-right corner char
    }
    GotoXY would take you to a specified (x, y) position on screen.
    SetTerminalColor would output the terminal escape sequences for the specified foreground and background colors.

    The above is my quick guess at a decent design for such a program/function. It may look like overkill, but decomposing a function like your original box() into all these little functions is actually good design. It keeps separate tasks separate and allows you to reuse code. It helps with debugging since you've narrowed down all your color-setting bugs to one small function, and with maintenance since you only have one small function to update if you wish to support, e.g., multiple terminals or change a color scheme, or allow a user-defined color scheme.

  8. #8
    Registered User
    Join Date
    Aug 2014
    Posts
    15
    What I needed to do was just take a break. I stopped and thought and then it hit me. I declare a 2d array and then I try to append to it. I need to at least give the first dimension. I was thinking I had a huge problem but it was just not giving the first dimension. Thanks for the help.

  9. #9
    Registered User
    Join Date
    Aug 2014
    Posts
    15
    And I like your idea of using structs for it. I'll do that. I'm planning to do a whole series of functions all on graphics. (text based) just so that I can understand everything. I think strings are one of the hardest things for a C++ programmer to understand about C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-12-2010, 07:02 PM
  2. Problem With sprintf and LPCTSTR?
    By ElWhapo in forum Windows Programming
    Replies: 2
    Last Post: 04-23-2005, 11:43 PM
  3. Interesting Problem: sprintf does not work
    By mangoMan in forum C++ Programming
    Replies: 15
    Last Post: 05-05-2004, 08:04 PM
  4. problem using sprintf
    By hanhao in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 07:37 AM
  5. sprintf problem
    By waxby in forum C Programming
    Replies: 4
    Last Post: 01-15-2003, 02:48 PM

Tags for this Thread


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