#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0

    Unhappy Adding element to multi-dimentional string array


    Hi,

    Please could someone help me with the code to add an element to an uninitialised array of strings in C.

    compiler warning: arithmetic on pointer etc

    Thank you!:(
  2. #2
  3. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    something like:
    Code:
    char Strings[10][80];
    int numstrings=0;
    strcpy(Strings[numstrings], "newstring");
    numstrings++;
    where 10 is the maximum number of strings, with a max size of 80 characters???
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Consider using the C++ vector, it can ease a lot of heartburn.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by mitakeet
    Consider using the C++ vector, it can ease a lot of heartburn.
    Can you post an example of how to use a C++ vector in C?
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Code:
    #include <stdio.h>
    
    #include <vector>
    #include <string>
    
    int main(){
        std::vector < std::string > myVector;
        char myCharInput[] = "char array input here";
    
        myVector.push_back("first string");
        myVector.push_back("second string");
        myVector.push_back(myCharInput);
    
        //you can read from a file into a buffer and place the
        //contents of the buffer (char array) into the vector
    
        //here is how to get it back out...
        printf("printing from a vector with an iterator:\n");
        std::vector < std::string >::iterator myIterator = myVector.begin();
        while (myIterator != myVector.end()){
            printf("\t%s\n", (*myIterator).c_str());
            myIterator++;
        }
    
        printf("\nprinting from a vector via direct access:\n");
        for (int i=0; i<myVector.size(); i++){
            printf("\t%s\n", myVector[i].c_str());
        }
    
        return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo