Thread: passing arrays

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    5

    Question passing arrays

    I'm having some trouble figuring out how to pass arrays. I need to write a function with 3 parameters: a,b,i. A and B are char arrays. The parameter i is an int. The function inserts the string a into b immediately after index i. Assume b is large enough to hold the added characters.
    So an example of what I'm trying to do is the following...
    A is "Today Wednesday"
    b is "is"
    i is 6
    and the result of the function would be
    Today is Wednesday

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    4
    I believe you can use pointers to pass the arrays if you donot know how to do so i can look up an example of such a code for you

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    // this is the prototype
    void insert_word( char line[], char word[], int pos );
    
    // example use
    int main ( ) {
        char buff[100] = "Today Wednesday";
        char insert[]  = "is";
        insert_word( buff, insert, 6 );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Null Shinji's Avatar
    Join Date
    Oct 2001
    Posts
    80
    you want something exact ne??? so it will be

    Code:
    memmove(a+i+strlen(b)-1, a+i,strlen(a)-i) 
    /*i'm not sure about this, i mean, i dont know if the arguments
    are in the correct order for this function, i used memmove(dest,source, num)*/
    memcpy(a+i,b,strlen(b)-1) 
    /*again used memcpy(dest,source, num)
    but i'm not sure about the order of the arguments*/
    Last edited by Null Shinji; 10-25-2001 at 02:48 PM.
    Null Shinji The Sorcerer is here
    Evangelion Quotes:
    "If youre gonna do it, dont waste time. Otherwise, leave", Gendo
    "Release the final safety lock, Evangelion Unit One, Lift Off!!", Misato
    "Syncrograph has reversed, pulses are flowing back!!!", Maya
    streamload id= nullshinji icq= 12944337; E-M@IL= [email protected]; aim= mayeba
    msn= [email protected]

  5. #5
    Registered User Null Shinji's Avatar
    Join Date
    Oct 2001
    Posts
    80
    if you want me to explain.... pm me or mail me at [email protected]
    Null Shinji The Sorcerer is here
    Evangelion Quotes:
    "If youre gonna do it, dont waste time. Otherwise, leave", Gendo
    "Release the final safety lock, Evangelion Unit One, Lift Off!!", Misato
    "Syncrograph has reversed, pulses are flowing back!!!", Maya
    streamload id= nullshinji icq= 12944337; E-M@IL= [email protected]; aim= mayeba
    msn= [email protected]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More trouble passing arrays
    By Rad_Turnip in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 08:11 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  4. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  5. passing arrays to functions
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-01-2002, 03:18 PM