Thread: Passing strings to functions?

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Angry Passing strings to functions?

    As the title implies i am trying to pass a string to a function, and i wondered if there is any way to do it without using pointers?

    Here is my situation
    I have a structure:
    Code:
    struct foot_item {
           char item_name[20];
           char item_class[10];
           char item_slot[10];
           int armor;
           int durability;
           int minimumlevel;
           int hpbonus;
           int mpbonus;
           int attackpowerbonus;
           int attackhitbonus;
           int spellpowerbonus;
           int spellhitbonus;
           };
    struct foot_item sabatons;
    And now i want to be able to initalize everything from a function
    Code:
    initialize_armor(sabatons,Plate,Feet,0,0,0,0,0,0,0,0,0);
    But i am having trouble passing the sabaton, plate and feet ones.
    forgot to write this, is it possible to use a variable if i want to modify a structure (sorry if i am a bit cryptic)
    something like?
    Code:
    variable.spellpowerbonus=20;
    Edit: Also, this is how the function looks like
    Code:
    void initialize_armor(char name, char item_class, char item_slot,
    int armor, int durability, int minimumlevel, int hpbonus, int mpbonus, 
    int attackpowerbonus, int attackhitbonus, int spellpowerbonus, int spellhitbonus);
    The reason i constructed my code this way was so that i could create a database of items (in this example boots)
    with different stats and properties, so i can have one pair of boots named "foot_item sabatons;"
    and the next maybe named "foot_item slippers;" but still with the same properties(ok, maybe not the same but the same kind of properties)
    if you have any better ideas on how to do it please post them as well - im trying to learn here
    Last edited by Afro; 09-30-2007 at 02:50 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Afro View Post
    As the title implies i am trying to pass a string to a function, and i wondered if there is any way to do it without using pointers?
    This question screams that you don't know what you're talking about.

    A string in C, with some exception or variation, is a sequence of numbers that end with a final ending 0. Generally, we use an array or a pointer to the first element of such a block of memory to represent such a concept.

    Since an array degenerates to a pointer to the first element of the array when passed to a function, you're forced to deal with an array-become-pointer or a straight pointer.

    Given that, you have to use pointers of some sort if you want to directly pass a string to a function, your question makes absolutely no sense.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Quote Originally Posted by MacGyver View Post
    Given that, you have to use pointers of some sort if you want to directly pass a string to a function, your question makes absolutely no sense.
    The question was if there actually WAS a way to pass them directly to a function without a pointer, so that part of your reply makes absolutely no sense aswell. Thank you for your help though.

    Could someone help me with this part as well?
    forgot to write this, but also, is it possible to use a variable if i want to modify a structure (sorry if i am a bit cryptic)
    something like?
    Code:
    VARIABLE.spellpowerbonus=20;
    Last edited by Afro; 10-01-2007 at 12:39 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The question was if there actually WAS a way to pass them directly to a function without a pointer
    Not without getting some other type involved. You can wrap the string in a structure and pass the structure by value:
    Code:
    struct passing {
      char string[1024];
    };
    
    void foo ( struct passing arg )
    {
      /* string was passed directly by value */
    }
    That's awkward and inflexible though.

    >Could someone help me with this part as well?
    I don't understand your question. That's naturally how you would modify the members of a structure instance provided "variable" refers to an instance of a structure with that member.

    >Since an array degenerates to a pointer to the first element of the array
    Why the pointer hate? What did they ever do to be thought of as lesser beings than arrays?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    I don't understand your question. That's naturally how you would modify the members of a structure instance provided "variable" refers to an instance of a structure with that member.
    I'll try to explain, let's say i have three structures all connected (dont know the word for it)
    to the foot_item structure.

    Code:
    struct foot_item {
           char item_name[20];
           char item_class[10];
           char item_slot[10];
           int armor;
           int durability;
           int minimumlevel;
           int hpbonus;
           int mpbonus;
           int attackpowerbonus;
           int attackhitbonus;
           int spellpowerbonus;
           int spellhitbonus;
           };
    struct foot_item sabatons;
    struct foot_item slippers;
    struct foot_item sneakers;
    I want to be able to use a variable to change, for example the selected boots armor value.
    Code:
    sneakers.armor=100;
    but instead of sneakers have a variable so it can be replaced, for example

    Code:
    //where variable is sneakers
    VARIABLE.armor=100;

    pretty much: is it possible to use a variable to select what structure you are going to modify?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    May I suggest pointers, and no that's not a pun.

    You can do something like this:
    Code:
    struct foot_item sneaker;
    struct foot_item boot;
    struct foot_item sandal;
    ...
    struct foot_item *current_foot_item;
    ... 
    void updgrade_foot_item_armor(int improvement)
    {
       current_foor_item->armor += improvement;
    }
    
    void wear_foot_item(struct foot_item *footwear)
    {
       current_foot_item = footwear;
    }
    ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    struct foot_item sabatons;
    struct foot_item slippers;
    struct foot_item sneakers;
    
    struct foot_item * variable;
    variable = &sneakers;
    variable->armor = 100;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is it possible to use a variable to select what structure you are going to modify?
    Not really. What you're talking about is a symbol map, where a string maps to specific symbol. It can be done, of course, but it's somewhat awkward and you would be better off using more conventional solutions (like an update function or a pointer that changes what object it points to depending on what needs updating) or switching to a language that supports this kind of thing.
    My best code is written with the delete key.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Afro View Post
    The question was if there actually WAS a way to pass them directly to a function without a pointer, so that part of your reply makes absolutely no sense aswell. Thank you for your help though.
    If you understood strings you would not have asked the question, at least not in the manner that you did.

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Quote Originally Posted by MacGyver View Post
    If you understood strings you would not have asked the question, at least not in the manner that you did.
    I understand, i'm just tired and got a bit ticked off by what you said, a simple "no, that is impossible" would've been enough. And yes, i am quite new to C. But it was apparently obvious eh? :P
    Thank you all for all the help though, i think i've got it sorted now!

    Oh, edit: I do know that strings are in essence arrays but i thought that some clever person had probably come up with some sort of workaround, that might not class as understanding strings though.

  11. #11
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Afro View Post
    I understand, i'm just tired and got a bit ticked off by what you said, a simple "no, that is impossible" would've been enough. And yes, i am quite new to C. But it was apparently obvious eh? :P
    Thank you all for all the help though, i think i've got it sorted now!

    Oh, edit: I do know that strings are in essence arrays but i thought that some clever person had probably come up with some sort of workaround, that might not class as understanding strings though.
    In another attempt to sound like you know what you saying, you trip over you words and break your nose.

    No one was really talking about strings. The word "strings" is often used to refer to a character array, but it can also be used to refer to the String class from C++.

    Even though you used the incorrect terms in the wrong order - Yes, Strings in C++ are essentially character arrays.

    Anyway, what you probably meant was that arrays are essentially pointers when it comes down to it. Which is right.

    By the way - there's nothing wrong with being new and not knowing everything. Trying to sound like you do when you don't will most likely be considered wrong by most people, though.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Afro View Post
    I understand, i'm just tired and got a bit ticked off by what you said, a simple "no, that is impossible" would've been enough. And yes, i am quite new to C. But it was apparently obvious eh? :P
    Thank you all for all the help though, i think i've got it sorted now!

    Oh, edit: I do know that strings are in essence arrays but i thought that some clever person had probably come up with some sort of workaround, that might not class as understanding strings though.
    You can do it indirectly, as I believe was pointed out, with a struct, but that is more complicated and not as efficient or meaningfull unless you have a reason to do so. Even if you do use a struct, you'd increase performance (if only a fraction of a fraction of a fraction.... yeah) should you pass a pointer of it to the function, hence you'd still be dealing with pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  2. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  3. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  4. Passing from functions in classes to another function in another class?
    By Robert_Sitter in forum Windows Programming
    Replies: 1
    Last Post: 12-13-2005, 07:46 AM
  5. passing strings from functions as the "return" part
    By fraktal in forum C Programming
    Replies: 8
    Last Post: 12-13-2005, 01:38 AM