#include <iostream>
#include <string.h>
usingnamespace std;
int main (int argc, char * const argv[]) {
size_t len,size,blanks;
size = 0;
char *strArray[10];
char *result;
char line[] = "ls -l -a l wc -c >> myfile";
len = strlen(line);
cout << "The length of the line is " << len << endl;
// Tokenize string "line" using space as delimiter. Stores information in char string array strArray
char *hold;
hold = strtok(line, " ");
strArray[0]=hold;
for (int i = 1;i<10; i++) {
hold = strtok(NULL, " ");
strArray[i] = hold;
printf("%s\n",strArray[i]);
}
// Tallies the size of each piece of strArray into variable 'size'
for (int i = 0; i<10; i++) {
size = size + strlen(strArray[0]);
}
cout << size << endl;
// Calculates the number of spaces by subtracting the 'size'(has no spaces) from the 'len'(has spaces)
blanks = len - size;
// Use new
char *p1 = newchar[size+1];
result = p1;
cout << "The result is " << result << endl;
char *ls;
ls = strArray[0];
printf("\n%s",ls);
char p2[100];
for (int i = 0; i<10; i++) {
strcat(p2, strArray[i]);
}
cout << hold << endl;
return 0;
}
I keep running into problems with the above section of code. I am admittedly weak in my understanding of how pointers work, but I have looked up information on many of the subjects and cannot grasp what is wrong. It seems that I only get a segmentation fault whenever I try to access the an element of strArray[i] in any for loop.
If I haven't been clear enough about my problem, please ask and I'll try and be more clear.