hi, to you all
i am having problem on this:
how do i initialize a char array, so i'll be able to put words in it at a runtime?
does anyone have an example of how arrays of char or strings work on C?
thanks in advance
hi, to you all
i am having problem on this:
how do i initialize a char array, so i'll be able to put words in it at a runtime?
does anyone have an example of how arrays of char or strings work on C?
thanks in advance
>>how do i initialize a char array, so i'll be able to put words in it at a runtime?
Do you want to initialize it or do you want to take it at run time? If you initialize it, you will fix the size and if you want to take the value at run time, you will need to allocate memory at run time. You can ofcourse use realloc().
Anyways.
to initialize,
char str[] = "hello world"; // or whatever string you want
else
char *str;
..... str = (char *) malloc (n*sizeof(char) ) // or whatever length you want to use and then use strcpy.
Hope that helps.
Anoop.
well, i am a little confused here, but what i really want is:
i have a database and a column from which i will take the fields values and put them all in an array.
i already know how to take a value from the database and store it in a char pointer. but i can not take all the values from that column and put them in a string array.
imagine the following table:
-------------------------
| ID | ITEM CODE|
-------------------------
| 01 | banana |
-------------------------
| 02 | apple |
-------------------------
| 03 | orange |
-------------------------
...
and so on...
i already know how to take a code from one of the registries and put it in a char*
but the result i expect is an array with all the items , like:
Code:array = {"banana","apple","orange"};
sorry, i dont know if i made myself clear, but do you know how to make this?
If you know the size, which I am sure, you wont, you could have declared a double array.
however you can either declare char **str; and simulate it or you can declare a linked list.
A little busy, else would have posted some code.
Anoop.
You could declare an array of strings. If there is a maximum string length and a maximum number of records on the database, you could declare something like this:
char database [MAX_NR_OF_RECORDS][MAX_STRING_LENGTH];
It is not very efficient, if your database its maximum size is quite large, but in practice the database uses just a small part of the maximum capacity, you have a lot of unused memory. So you could also consider anoopks' solution and use some datastructure for which you will allocate memory dynamically, like a linked list.
ok, i'll try that. thank you guys!!!
Use array of Character pointers
Saravanan.T.S.
Beginner.
how do initialize and use an array of char pointers?
>how do initialize and use an array of char pointers?
Maybe something like this.Code:#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { const char filename[] = "file.txt"; FILE *file = fopen(filename, "r"); if ( file != NULL ) { char buffer[BUFSIZ]; char *array[10]; size_t i,j; for ( i = 0; i < sizeof(array)/sizeof(*array); ++i ) { if ( fgets(buffer, sizeof buffer, file) != NULL ) { char *newline = strchr(buffer, '\n'); if ( newline != NULL ) { *newline = '\0'; } array[i] = malloc(strlen(buffer) + 1); if ( array[i] == NULL ) { exit(EXIT_FAILURE); } strcpy(array[i], buffer); } else { break; } } fclose(file); printf("%2s %s\n", "ID", "ITEM CODE"); for ( j = 0; j < i; ++j ) { printf("%02lu %s\n", (long unsigned)(j + 1), array[j]); free(array[j]); } } return 0; } /* file.txt apple banana orange */ /* my output ID ITEM CODE 01 apple 02 banana 03 orange */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*