2D arrays and pointers - c

Hey All, I am a pointer newbie and in the following code, I am trying to store the values of a 2 D array in a structure and then print them. However, I get a compilation error at the line: fd->mychar[i] = newptr[i]; I get that while char * str is same as str[], char ** str isnt the same as str[][], but I cant find a solution to make the following work.
typedef struct mystruct{
char mychar [20][20];
}mystruct_t;
void printvalues ( char ** newptr){
int i;
mystruct_t * fd;
for (i=0;i<3;i++){
fd->mychar[i] = newptr[i];
printf("My value is %s and in struct %s\n", newptr[i], fd->mychar[i]);
}
}
int main (int argc, char **argv){
int i;
char * abc[5] = {"123", "456", "789"};
for (i=0;i<3;i++){
printf("My value is %s\n", abc[i]);
}
printvalues(abc);
}

Most of the issue was your use of an unallocated structure.
You used a pointer to mystruct_t but never allocated it.
The following runs for me:
#include <stdio.h>
typedef struct mystruct
{
char* mychar [20];
} mystruct_t;
void printvalues( char** newptr )
{
int i;
// note: you used an unallocated pointer in your original code
mystruct_t fd;
for ( i = 0; i < 3; i++ )
{
fd.mychar[i] = newptr[i];
printf( "My value is %s and in struct %s\n", newptr[i], fd.mychar[i] );
}
}
int main( int argc, char **argv )
{
int i;
char * abc[5] = { "123", "456", "789" };
for ( i = 0; i < 3; i++ )
{
printf( "My value is %s\n", abc[i] );
}
printvalues( abc );
}

Related

How to access to 2D array of char* through structure?

Here is a 2D array of char*, storing for example different strings in different languages :
typedef enum
{
FRENCH,
ENGLISH,
GERMAN,
LANGUAGES_COUNT
} languages_t;
typedef enum
{
HELLO,
THANK_YOU,
WORDS_COUNT
} words_t;
char *text_tab[WORDS_COUNT][LANGUAGES_COUNT] =
{
{"bonjour", "hello", "guten tag"},
{"merci", "thank you", "danke"}
};
No trouble to access it :
int main()
{
printf("%s\n", text_tab[HELLO][ENGLISH]);
printf("%s\n", text_tab[THANK_YOU][FRENCH]);
printf("%s\n", text_tab[HELLO][GERMAN]);
return 0;
}
Now, I don't want to access text_tab directly, but through a structure :
typedef struct
{
int a;
char ***txt; // here is not working
} test_t;
test_t mystruct = {5, text_tab};
The idea is to access text_tab this way :
printf("%s\n", mystruct.txt[BONJOUR][ANGLAIS]);
printf("%s\n", mystruct.txt[MERCI][FRANCAIS]);
printf("%s\n", mystruct.txt[BONJOUR][ALLEMAND]);
How to declare the field "txt" in the structure ?
I only use static allocation and I don't want to copy the content of "text_tab" in "txt", juste use a pointer.
Thanks.
This array
char *text_tab[WORDS_COUNT][LANGUAGES_COUNT] =
{
{"bonjour", "hello", "guten tag"},
{"merci", "thank you", "danke"}
};
has the type char * [WORDS_COUNT][LANGUAGES_COUNT].
So a pointer to elements of the array has the type char * ( * )[LANGUAGES_COUNT]
Hence the structure can be declared like
typedef struct
{
size_t a;
char * ( *txt )[LANGUAGES_COUNT];
} test_t;
And in main you can declare an object of the structure type like
test_t mystruct = { sizeof( text_tab ) / sizeof( *text_tab ), text_tab };
Here is a demonstrative program.
#include <stdio.h>
#define WORDS_COUNT 2
#define LANGUAGES_COUNT 3
typedef struct
{
size_t a;
char * ( *txt )[LANGUAGES_COUNT];
} test_t;
int main(void)
{
char *text_tab[WORDS_COUNT][LANGUAGES_COUNT] =
{
{"bonjour", "hello", "guten tag"},
{"merci", "thank you", "danke"}
};
test_t mystruct = { sizeof( text_tab ) / sizeof( *text_tab ), text_tab };
for ( size_t i = 0; i < mystruct.a; i++ )
{
for ( size_t j = 0; j < sizeof( *mystruct.txt ) / sizeof( **mystruct.txt ); j++ )
{
printf( "%s ", mystruct.txt[i][j] );
}
putchar( '\n' );
}
return 0;
}
The program output is
bonjour hello guten tag
merci thank you danke

how to print the contents of char**?

I have a structure defined as a char** array containing strings. I dont know how to run printf on its contents.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef STRUCT_STRING_ARRAY
#define STRUCT_STRING_ARRAY
typedef struct s_string_array
{
int size;
char** array;
} string_array;
#endif
void my_print_words_array(string_array* param_1)
{
int len = param_1->size;
char **d = param_1->array;
for(int i = 0 ; i < len;i++){
printf("%s\n", d[i]);
}
}
int main(){
struct s_string_array *d;
d->size = 2;
char **my_arr = (char *[]){"hello", "world"};//this init is fine
d->array = my_arr;
my_print_words_array(d);
return 0 ;
}
the main function gives me segfault error. What's wrong?
There is no sense to declare a pointer to the structure
struct s_string_array *d;
moreover that is not initialized and has indeterminate value that further is a reason of undefined behavior.
What you are trying to achieve is the following
#include <stdio.h>
typedef struct s_string_array
{
int size;
char** array;
} string_array;
void my_print_words_array( const string_array *param_1 )
{
for ( int i = 0; i < param_1->size; i++ )
{
puts( param_1->array[i] );
}
}
int main( void )
{
string_array d =
{
.size = 2,
.array = (char *[]){"hello", "world"}
};
my_print_words_array( &d );
return 0 ;
}
The program output is
hello
world

Programming in C: Changing member value of struct gives segfault

There are quite a few topics on this subject but I haven't been able to find a solution that's worked for me; I am getting a segmentation fault whenever I try to change student_t.member->name. Below is the typedef structure that I am required to use:
typedef struct degree_t degree_t;
typedef struct student_t student_t;
struct degree_t {
student_t* member;
int course_id;
}
struct student_t {
char* name;
int age;
}
int main(int argc, char* argv[]) {
student_t *students = malloc(sizeof(student_t) * 3);
degree_t *degrees = malloc(sizeof(degree_t));
for (int i=0; i<3; i++) {
degrees[i].course_id = 1;
degrees[i].member->name = "Bob";
}
return 0;
}
I can change degrees[i].course_id perfectly fine, but whenever I try to change degrees[i].member -> name, I get a segmentation fault.
GDB indicates that this is a result of the line degrees[i].member->name = "Bob", but I'm don't understand why, and I don't know how to effectively change the value of degrees[i].member->name
Oops, you managed to place four errors in that small program.
The signature of main() should be main(int argc, char *argv[]). argv is an array of strings found on the command line.
You allocate memory for one degree, but in the for-loop you initialize three of them. This leads to a heap corruption.
You don't initialize degrees->member, but use it.
You allocate memory for three students, but don't use it.
Try this:
struct degree_t {
student_t* member;
int course_id;
}
struct student_t {
const char* name;
int age;
}
int main(int argc, char* argv[]) {
degree_t *degrees = malloc(3 * sizeof(degree_t));
for (int i=0; i<3; i++) {
degrees[i].course_id = 1;
degrees[i].member = malloc(sizeof(student_t));
degrees[i].member->name = "Bob";
degrees[i].member->age = 23;
}
return 0;
}

C - Pass Array of Strings as Function Parameter

I need to pass a pre-allocated array of strings as a function parameter, and strcpy() to each of the strings within the string array, as in this example:
static void string_copy(char * pointer[]) {
strcpy(pointer[0], "Hello ");
strcpy(pointer[1], "world");
}
int main(int argc, const char * argv[]) {
char my_array[10][100];
string_copy(my_array);
printf("%s%s\n", my_array[0], my_array[1]);
}
And the resulting printed string would be 'Hello world'.
How do I pass a pre-allocated string array and fill out each string within a function as shown above?
When you are doing string_copy(my_array), you are passing a char (*)[100], i.e. pointer to char[100] array to your function. But your function is expecting a char *[], i.e. array of char pointers, because you have defined your function that way.
You can fix this by making changes so that your function (string_copy()) expects a char (*)[100], instead of a char *[].
For this, you can change your function definition as:
/* Your my_array gets converted to pointer to char[100]
so, you need to change your function parameter
from `char *pointer[]` to `char (*pointer)[100]`
*/
/* static void string_copy(char *pointer []) */
static void string_copy(char (*pointer) [100])
{
strcpy(pointer[0], "Hello ");
strcpy(pointer[1], "world");
}
* Alternative Solution *
A different design/solution would be to change in your main() function so that you are actually passing a char *[], which decays into a char ** - which is fine - to string_copy(). This way you would NOT have to change your string_copy() function.
int main(int argc, const char * argv[]) {
char my_array[10][100];
int tot_char_arrs, i;
char *char_arr_ptr[10];
/* Get total number of char arrays in my_array */
tot_char_arrs = sizeof(my_array) / sizeof(my_array[0]);
// Store all char *
for (i = 0; i < tot_char_arrs; i++)
char_arr_ptr[i] = my_array[i];
/* Actually passing a char *[].
it will decay into char **, which is fine
*/
string_copy(char_arr_ptr);
printf("%s%s\n", my_array[0], my_array[1]);
}
you need to use a pointer to the array. here is an example with 1 dimension array:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
static void string_copy(char **pointer) {
strcpy(pointer[0], "Hello ");
}
int main(int argc, const char * argv[]) {
char my_array[10];
char * p_array = my_array;
string_copy(&p_array);
printf("%s\n", my_array);
}
Your function can simply accept matrix dimensions and pass a const char * that stores the array of literals (pre-allocated) strings:
#include <stdio.h>
#include <string.h>
#define STRINGS_LENGTH 100
static void string_copy(size_t n, size_t m, char pointer[n][m], const char *strings_to_copy[])
{
for (size_t i=0; i< n; i++)
{
strcpy(pointer[i], strings_to_copy[i]);
}
}
int main( void )
{
const char *strings[] = { "hello", "World" };
char my_array[sizeof(strings)/sizeof(strings[0])][STRINGS_LENGTH];
string_copy(sizeof(strings)/sizeof(strings[0]), STRINGS_LENGTH, my_array, strings);
printf("%s %s\n", my_array[0], my_array[1]);
}
You can also change the structure of your code using dynamic allocation for your output array like:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
static bool string_copy(char *pointer[], const char *strings_to_copy[], size_t strings)
{
for (size_t i=0; i< strings; i++)
{
pointer[i] = malloc(strlen(strings_to_copy[i])+1);
if (pointer[i] != NULL)
strcpy(pointer[i], strings_to_copy[i]);
else
return false;
}
return true;
}
int main(void)
{
const char *strings[] = { "hello", "World" };
char *my_array[sizeof(strings)/sizeof(strings[0])] = {0};
if (string_copy(my_array, strings, sizeof(strings)/sizeof(strings[0])) )
{
printf("%s %s\n", my_array[0], my_array[1]);
}
for (size_t i = 0; i<sizeof(strings)/sizeof(strings[0]); i++)
free (my_array[i]);
}

C: Finding duplicate string values in a file

So I have a file containing lets say:
cat
dog
cat
I am trying to go through the file, have it recognize there are two cat elements and one dog element, and then have in the same file edited as:
cat - 2
dog - 1
I already have all the words saved in an array of strings, char **wordList, and I am trying to sort them with qsort and then put it in the format as described above. My qsort functions are:
stringcmp(const void *a, const void *b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return strcmp(*ia, *ib);
}
void wordSort(char **wordlist)
{
size_t strings_len = numwords - 1;
qsort(wordlist, strings_len, sizeof(char*), stringcmp);
wordFile(wordlist);
}
void wordFile(char **wordlist)
{
if((outFilePtr2 = fopen(outWords, "w")) != NULL)
{
for(x = 1; x < numwords; x++)
{
fputs(wordlist[x], outFilePtr2);
fputs("\n", outFilePtr2);
}
fclose(outFilePtr2);
}
else
{
printf("File\"%s\" could not be opened.\n", outWords);
}
}
It is not sorting anything in order though. How do I fix it?
The following program works with your definition of stringcmp (which seems correct):
int main (int argc, char *argv[]) {
int i;
qsort(argv, argc, sizeof(char *), &stringcmp);
for (i = 0; i != argc; i++) printf("%s\n", argv[i]);
}
Thus I suspect that you have a problem with the definition of char **wordList.
UPDATE
This version slightly modified/completed version of your program works for me:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *outWords = "outWords.txt";
char *wordList[] = { "cat", "dog", "cat" };
#define numwords (sizeof(wordList) / sizeof(wordList[0]))
FILE *outFilePtr2;
int x;
int stringcmp(const void *a, const void *b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return strcmp(*ia, *ib);
}
void wordSort(char **wordlist)
{
qsort(wordlist, numwords, sizeof(char*), stringcmp);
wordFile(wordlist);
}
void wordFile(char **wordlist)
{
if((outFilePtr2 = fopen(outWords, "w")) != NULL)
{
for(x = 0; x < numwords; x++)
{
fputs(wordlist[x], outFilePtr2);
fputs("\n", outFilePtr2);
}
fclose(outFilePtr2);
}
else
{
printf("File\"%s\" could not be opened.\n", outWords);
}
}
int main() {
wordSort(wordList);
wordFile(wordList);
return 0;
}
I adapted the second argument of qsort (else the last string pointer would not be considered, and left unchanged). I also adapted the initialization x=0 of the for-loop in wordFile for the first string to be printed, too.
You may have defined **wordList in some other way causing a problem, you did not provide the code for it.

Resources