im quite new to C, and im trying to populate a file wide array variable with the results of a function, heres a simple code sample to show what i mean, can anyone point me in the direction of why this doesnt work?
#include <sys/types.h>
#include <dirent.h>
#include <regex.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
static gchar *external_names;
void directories(int arraylength, gchar internal_names[][100]){
int n;
for (n = 0; n < arraylength; n++)
{
strcpy(external_names[n], internal_names[n]);
}
for (n = 0; n < arraylength; n++)
{
printf("%s internal with %s external\n",internal_names[n], external_names[n]);
}
}
void main()
{
gchar anotherarray[10][100];
directories(10, anotherarray);
}
[EDIT] latest code
#include <sys/types.h>
#include <dirent.h>
#include <regex.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
static gchar *external_names[100];
void directories(int arraylength, gchar internal_names[][100]){
int n = 0;
//gchar external_names[arraylength][100];
for (n = 0; n < arraylength; n++)
{
printf("%s %i\n","before", n);
strcpy(external_names[n], internal_names[n]);
printf("%s %i\n","after", n);
}
}
void main()
{
int n;
gchar anotherarray[10][100];
for (n = 0; n < 10; n++)
{
strcpy(anotherarray[n],"test");
}
directories(10, anotherarray);
for (n = 0; n < 10; n++)
{
printf("%s internal with %s external\n",anotherarray[n], external_names[n]);
}
}
external_names is a one dimensional array and you are trying to assign it from a two dimensional array internal_names.
This is what happens when you increment the index.
To solve this,
should work.