Array of strings... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Array of strings...

drew84drew84 Posts: 8Member
Hi, im pretty sure this is a very simple question but im very new to C and win api and im trying to figure out how to create an array of strings... ive used C++ and its very easy to do in it... but since there is "string" class that i could predefine im not sure how to do it...
right now im trying to use chars, but im not sure at all of how to create a array of strings and have it take input from a file... Any help would be greatly apprecaited thank you.

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    : Hi, im pretty sure this is a very simple question but im very new to C and win api and im trying to figure out how to create an array of strings... ive used C++ and its very easy to do in it... but since there is "string" class that i could predefine im not sure how to do it...
    : right now im trying to use chars, but im not sure at all of how to create a array of strings and have it take input from a file... Any help would be greatly apprecaited thank you.
    :
    [code]
    [red]if you know the maximum number of strings
    then this is the simplest way to do it.[/red]

    // maximum number of strings in the array
    #define MAX_NUMBER_STRINGS 1000
    // each string can contain this may characters
    #define MAX_STRING_SIZE 80
    char array[MAX_NUMBER_STRINGS][MAX_STRING_SIZE];

    [red]If you don't know how may strings you need until
    runtime, then you can do this:[/red]

    char **array;
    array = (char **)malloc(NumberStrings);
    for(i = 0; i < NumberStrings; i++)
    array[i] = malloc(MAX_STRING_SIZE);
    [/code]

  • drew84drew84 Posts: 8Member
    Okay i understand those two implementations but for the top one (multi-dmimensional array?) how do i access it.. im going through and reading a file line by line and i wanna assign the values to it, im reading the file originally with a:
    char buffer[255];
    do i say:
    array[size++][??] = buffer;
    not sure what to put in ??, max string size? and if i do put in max string size i get an error error C2440: '=' : cannot convert from 'char [255]' to 'char'
    i really dont know how to manipulate chars at all in C.. im used to C++ and java... thanks for your help so far.



    : [code]
    : [red]if you know the maximum number of strings
    : then this is the simplest way to do it.[/red]
    :
    : // maximum number of strings in the array
    : #define MAX_NUMBER_STRINGS 1000
    : // each string can contain this may characters
    : #define MAX_STRING_SIZE 80
    : char array[MAX_NUMBER_STRINGS][MAX_STRING_SIZE];
    :
    : [red]If you don't know how may strings you need until
    : runtime, then you can do this:[/red]
    :
    : char **array;
    : array = (char **)malloc(NumberStrings);
    : for(i = 0; i < NumberStrings; i++)
    : array[i] = malloc(MAX_STRING_SIZE);
    : [/code]
    :
    :

  • WaltPWaltP Posts: 335Member
    : Okay i understand those two implementations but for the top one (multi-dmimensional array?) how do i access it.. im going through and reading a file line by line and i wanna assign the values to it, im reading the file originally with a:
    : char buffer[255];
    : do i say:
    : array[size++][??] = buffer;
    : not sure what to put in ??, max string size? and if i do put in max string size i get an error error C2440: '=' : cannot convert from 'char [255]' to 'char'
    : i really dont know how to manipulate chars at all in C.. im used to C++ and java... thanks for your help so far.

    You have a loop or line counter [b]lp[/b] which keeps track of the lines read, then
    [b]strcpy(array[lp], buffer);[/b]
    will copy the buffer read to the array of strings.

    Include string.h to define the string routines

    ----------------
    Walt


Sign In or Register to comment.