Reputation: 83
So I want to send an input like
ls -l | ./a.out
to the following program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
char virtualLs[100];
char eachLineOfLsInArray[100][100];
scanf("%[^\t]", virtualLs);
char *eachLineOfLs;
eachLineOfLs = strtok(virtualLs, "\n");
int loopCounterForStuffing;
loopCounterForStuffing = 0;
while (eachLineOfLs != NULL)
{
strcpy(eachLineOfLsInArray[loopCounterForStuffing], eachLineOfLs);
eachLineOfLs = strtok(NULL, "\n");
++loopCounterForStuffing;
}
char newLsArray[100][sizeof(eachLineOfLsInArray) / sizeof(char)];
int i;
for(i = 0; i < sizeof(eachLineOfLsInArray) / sizeof(char); i++)
{
char *array[10];
int k=0;
array[k] = strtok(eachLineOfLsInArray[i], " ");
while(array[k] != NULL)
{
array[++k] = strtok(NULL, " ");
}
newLsArray[i] = array;
printf ("res[%d] = %s\n", i, array[i]);
}
I get the following error message in compilation
largest.c:31:17: error: array type 'char [10000]' is not assignable
newLsArray[i] = array;
~~~~~~~~~~~~~ ^
1 error generated.
The purpose of this program is to put each line in to an array that contains its word as an array.
Upvotes: 0
Views: 110
Reputation: 1063
When you define an array like char[10] his type is char [10] not char* and so array
is of type char[10] while newLsArray[i]
is of type char [sizeof(eachLineOfLsInArray) / sizeof(char)]
which is different and so you get a type error, you can either use strcpy
or work with pointers and malloc
command and that way the line newLsArray[i] = array;
would work.
To work with pointers you will need to define an array as char* and 2-d array as char** and malloc them accordingly.
Upvotes: 1
Reputation: 3698
Use the strcpy
function:
Instead of
newLsArray[i] = array;
write
strcpy(newLsArray[i], array);
Upvotes: 1