I have to do a task for university c course and I'm stuck (i won't ask you to write it for me, it's just a simple question). I need to:
- malloc an array of pointers in which i'll save adresses of memory assinged to strings, which i'll enter
- activate function n times (n-number of strings) in which i enter the string and save it to its assigned memory
- activate function which sorts (with bubblesort) the array of pointers in alphabetical order of entered strings.
- print sorted strings.
i'm stuck here:
void enter(char *pointer[], int b, int l)
{
char temp[l];
printf("Enter string: ");
scanf("%s",temp);
printf("Test: %s; %d \n",temp, &temp);
pointer[b]=temp;
printf("Test: %s; %d \n",pointer[b], &pointer[b]);
}
void druga()
{
int i, ile=1, leng=100;
char *wsk[ile];
for(i=0;i<ile;i++) wsk[i]=(char*)malloc(leng*sizeof(char));
for(i=0;i<ile;i++)
{
enter(wsk, i, leng);
}
for(i=0;i<ile;i++)
{
printf("Test2 %s; %d \n", wsk[i], &wsk[i]);
}
//bubblesort(wsk);
}
int main(void)
{
druga();
return 0;
}
Output:
Enter string: asd
Test: asd; -402654896
Test: asd; -402654688
Test2 ; -402654688
The issue is that somehow the string does not pass to druga()
function.
Does anyone have a solution? Thanks for help