Creating a 2D array of strings in C? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Could also be called a 3D array, as each string is essentially it's own array.
Would be in the following format, each row = a new 'order'.
[ [FirstName, SecondName, DOB, Newspaper] ]
[ [FirstName, SecondName, DOB, Newspaper] ]
The contents of the array will be all strings (even though DOB would be in the format "23012017" and then converted to integer if necessary
Tried to use this "pointer arrays", but am not sure about how to use it.
char *bookings[][2];
char firstname[20], secondname[20], dob[8];
char *bookings[][0]=firstname, *bookings[][1]=secondname, *bookings[][2]=dob;

I think what you need is a struct and a normal 1D array. Like:
struct order {
char FirstName[42];
char SecondName[42];
char DOB[42];
char Newspaper[42];
}
and in your code (e.g. in main)
struct order[42];
Then you do:
strcpy(order[0].FirstName, "Donald");
strcpy(order[0].SecondName, "Duck");
... and so on

Related

How to concatenate LPTSTR with a char array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
BOOL test(LPTSTR a) {
char res[1024];
strcat(res,"before");
strcat(res,a); //<--
strcat(res,"after");
printf("%s",res);
}
How to concatenate LPTSTR with a char array?
Edit:
I have another functions which is returning a string in LPTSTR format. i cannot change this functions.
The problem is that the statement strcpy(res,a) does not compile. Says thats "LPTSTR" and "const char *" is not compatible.
Beside of this you are all right there are more problems with this snippet, but its not real just for showing the context of the strcpy statement.
Main problem is how to convert a lptstr to const char *, to can concatenate.
There are some issues in your code:
BOOL test(LPTSTR a) {
char res[1024];
strcat(res,"before"); // you're contatenating a string to res which is not
// initialized and which therefore contains garbage
strcat(res,a);
strcat(res,"after");
printf("%s",res);
} // test is declared to return a BOOL but you
// don't return anything, the compiler should warn you
You probably want this:
void test(LPTSTR a) {
char res[1024];
strcpy(res,"before");
strcat(res,a);
strcat(res,"after");
printf("%s",res);
}
Is there a reason why you use LPTSTR? Do you know what it is? The subject is quite vast. Probably you want to forget about LPTSTR and just use char*:
void test(char *a)
...
But without more information it's hard to tell.

c - char array only prints first char [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
This is just a part of the code and it is simplified for this question.
I would like to be able to store the word into my struct, the whole word not just the first char.
struct lexics{
enum token token;
char lexeme[LEXEME_MAX];
};
int main(void) {
char a[]="";
a[0]='w';
a[1]='h';
a[2]='i';
a[3]='l';
a[4]='e';
struct lexics rs={WHILE_KEYWORD,*a};
printf("%s\n",rs.lexeme);
}
this only prints w and I need it to print while
I cannot use anything other than the char a[]="";
a must be build in this way
When I print result it is while but when I put inside the struct it is only w
By doing struct lexics rs = {WHILE_KEYWORD, *a};, you are effectively putting the first element of a inside of the lexics.lexeme array. Try using a function that copies the entire array:
struct lexics rs = {WHILE_KEYWORD};
strncpy(rs.lexeme, a, LEXEME_MAX - 1);
rs.lexeme[LEXEME_MAX - 1] = 0; // to be sure that the string is properly terminated :)
In your code
char result[]="";
does not give you an infinite-length array, it's a one element array (only null-terminator). You need to either have a proper size mentioned, or a long-enough initializer to have a proper size of the array.
struct lexics{
enum token token;
char lexeme[LEXEME_MAX];
};
...
struct lexics rs={WHILE_KEYWORD,*a};
printf("%s\n",rs.lexeme);
In C arrays are filled with zero if there are less initializers than elements in the array.
In your code you only provide 1 single char to initialize char lexeme[LEXEME_MAX] array. This means the array looks like "w\0\0\0\0..." and as a result only the first character is printed.
You must use strcpy or strncpy to copy the string from array a into the array rs.lexeme.

C - Array of structures [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
im having a txt file
abc=123 def=456 ...
I have defined my struct like this:
typedef struct rule {
char* old;
char* new;
}Rule;
I have counted the ammount of these rules via function
int count_rules();
Now I'm calling this function in another, while making dictionary of rules
void make_dic(){
ammount_rules = count_rules();
//here goes the problem
Rule *dictionary = malloc(ammount_rules * sizeof(Rule));
}
I want to scan another txt and replace old with new so I'd like to acces every twin with simple command
for (i=0; i<ammount_rules;i++){
if ( (string_in_text) == (dictionary.old[i]) )
{
printf("%s" dictionary.new[i]);
}
}
Your malloc seems to be fine. It allocates an array of Rule with ammount_rules elements.
However, the way you use dictionary seems wrong.
dictionary.old[i] // wrong
dictionary[i].old // correct
dictionary.new[i] // wrong
dictionary[i].new // correct
BTW: Notice that you are comparing pointers - not strings. Use strcmp for comparing strings.

Allocating memory to ** [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this struct
typedef struct{
char **palavras;
}no;
and I want to allocate memory to this array of strings basically
and I can't do this, since it says it expects something before the '('
no *atual;
atual->(*palavras)=calloc(1,sizeof(char*));
You need to do it in several stages:
First, allocate memory to atual,
Next, allocate memory to palavras
Finally, allocate memory to elements of palavras
Assuming that you need to allocate 10 palavras, you can do it like this:
no *atual = malloc(sizeof(no));
atual->palavras = malloc(sizeof(char*)*10);
atual->palavras[0] = malloc(20);
...
You should access palavras by atual->palavras, e.g. atual->palavras = calloc(5, sizeof(char *)), and dereference the char ** by *atual->palavras. You may also allocate memory for the char * pointers by atual->palavras by, say, atual->palavras[0] = malloc(10 * sizeof(char)).

Can't understand program (sorting of array of structures) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a code: array of structures that must be sorted. Program works, but:
I can't understand, if Mat is pointer, why not void sort(tArt *sMat), but void sort(tArt sMat[]). I'm really puzzled.
typedef struct{
char data[26];
}tArt;
...
int main(void)
{
FILE* fMat; fMat=fopen..........
tArt* Mat;
...
Mat=malloc(sizeof(tArt));
for(i=0;i<N;i++) fread(&Mat[i],sizeof(tArt),1,fMat);
fclose(fMat);
sort(Mat,N);
...
}
void sort(tArt sMat, int num) {...........}
My guess since we can not see the entire code, is that when you use malloc to dynamically allocate the array you forget to allocate the array for N 'objects'. In other words, I suspect your problem lies in the line
Mat = malloc(sizeof(tArt));
where it should be
Mat = malloc(sizeof(tArt) * N);
On the other hand, when you create explicitly your array with a declaration of the form
tArt Mat[N];
where N is defined somewhere earlier in the ellipses, everything is working as expected.
Hope this helps.
There is no difference between tArt *sMat and tArt sMat[] and tArt sMat[1234], the compiler treat them all as tArt *sMat and ignore the length information.

Resources