Warnings from String manipulation functions.

This is a discussion on Warnings from String manipulation functions. within the C Programming forums, part of the General Programming Boards category; I'm using various standard string.h functions in a program that I am writing although I am obviously using them incorrectly. ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    21

    Warnings from String manipulation functions.

    I'm using various standard string.h functions in a program that I am writing although I am obviously using them incorrectly. Although the program seems to work with these compiler errors (so far) I know that they indicate something is wrong and that the results expected to be returned may be undefined in some cases. I will try to show the appropriate lines of code in here so that you can see. The code as a whole is fairly long, so I will spare you all and just post little bits.

    Code:
    /* these are globals at the moment */
    struct product
    {
    	float price;
    	char *descript[80];
    	int quantity;
    	char *sku[5];
    }
    items[MAX_ITEMS];
    
    */ snip /*
    
    /* here I simply try to compare the contents of a char string
     and a string literal? I store the result in the int j so that I can test the results
     of the compare /*
    
    int j; 
    j = strcmp(items[num_lines].sku, "q");
    
    /* same type of deal here except with a strcopy. I am trying to copy the contents of 
    the string desc, into the descript string (part of the product structure */
    
    char desc[100];
    strcpy (items[num_lines].descript, desc);
    
    /* one final example. I open a file and try to  write the contents of the product structure 
    to it, one attribute per line. Below I attempt 
    to write a char string to the file. */
    
    FILE *fp;
    fputs(items[counter].sku, fp);
    The warnings I get are like this... I'll post the three separate warnings that I get from the lines above. I get many more similar warnings than the ones above, but I think that if I can get some guidance on these, then I should be able to figure the rest out for myself.

    Code:
    register3.c: In function `main':
    register3.c:82: warning: passing arg 1 of `strcmp' from incompatible pointer type
    register3.c: In function `display_item':
    register3.c:225: warning: passing arg 1 of `strcpy' from incompatible pointer type
    register3.c:308: warning: passing arg 1 of `fputs' from incompatible pointer type
    Thanks guys, I apologize if this crap is difficult to read. If more info is required, I will happily provide.

    Cheers
    Arker.

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >j = strcmp(items[num_lines].sku, "q");
    strcmp() takes the first argument as a pointer to a string and not a pointer to pointer to string, which is in your case. So, your code should look something like:
    Code:
    j = strcmp(items[num_lines].sku[0], "q");
    But don't forget to allocate memory to each of the items[num_lines].sku[x]!

    >fputs(items[counter].sku, fp);
    Same applies here.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    21
    Okay, maybe this is not as simple as I thought it would be... Or maybe I'm not as clever as I thought I was... Either way, I'm afraid I need more guidance.

    When I try the code:
    Code:
    j = strcmp(items[num_lines].sku[0], "q");
    It immediately segfaults. You knew it was going to do that because you said:
    But don't forget to allocate memory to each of the items[num_lines].sku[x]!
    This is where I am lost (I'm sure you aren't suprised..lol). Is it not true that when I declare the sku string with
    Code:
     char *sku[5];
    the memory is allocated then? By the results of my experimentation with your code suggestion, I'm guessing not.
    So basically, in order to successfully put a string into the char *sku[5] field from user input, I need to create some sort of getchar() loop, filling each index of the sku field one user keystroke at a time while mallocing enough memory to hold a char data type with each iteration? At the moment, I am taking user input to fill the sku field with a simple scanf statement (with a %s).

    I hope my confusing questions are clearer to you than they are to me. I admire you folks for understanding these newb-type questions...hehe

    Cheers,
    Arker.
    Last edited by Arker; 10-14-2002 at 09:48 AM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Depending on what you're trying to hold the the sku variable you could do one of the following to get round the memory allocation problems for now:

    >char sku[5];
    ... will give you a 5 byte array for storing a 4 byte string

    >char sku[5][20];
    ... will give you 5, 20 byte arrays for storing 5 19 byte strings.

    Pick one, and ask again if you don't understand.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >the memory is allocated then?
    No. Look closely what you have declared: char *sku[5];
    The statement only impleis, you have declared 5 pointers, pointing somewhere in memory (probably some other program's address space, that's why you get the 'segfault' when you try to manipulate this!). Now, it's your duty to make these pointers point somewhere in YOUR program space, where you have allocated memory. That's where malloc() can help you. You can do:
    Code:
    sku[0] = malloc(100);
    sku[1] = malloc(100);
    sku[2] = malloc(100);
    sku[3] = malloc(100);
    sku[4] = malloc(100);
    Where, 100 bytes are allocated for each of the pointers.

    If you are not very clear with this, you can go by the simpler method as suggested by Hammer:
    Code:
    char sku[5][20];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Creating String Functions
    By stickman in forum C++ Programming
    Replies: 8
    Last Post: 04-30-2006, 05:26 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Best practice: string manipulation functions
    By samadhi in forum C Programming
    Replies: 4
    Last Post: 09-23-2004, 05:16 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM

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