Quote:
Originally Posted by Salem
so is
not needed?Code:another quick question
Code:
for(i = 0; i < num_of_inputs; i++)
{
Inputs[i] = malloc(cols * sizeof(char));
}
Printable View
Quote:
Originally Posted by Salem
so is
not needed?Code:another quick question
Code:
for(i = 0; i < num_of_inputs; i++)
{
Inputs[i] = malloc(cols * sizeof(char));
}
new code but it still prints out junk then seg faultsCode:#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/wait.h>
typedef enum
{
FALSE = 0,
TRUE = 1
} Bool;
int main()
{
ssize_t readReturn = 0;
size_t bufLen = 0;
char **Inputs = NULL;
void *temp;
char *input = NULL;
int x = 0;
Bool stillGoing = TRUE;
int num_of_inputs = 0;
int cols = 0;
int old_cols = 0;
int i = 0;
while(stillGoing)
{
printf("enter text: ");
readReturn = getline(&input, &bufLen, stdin);
for(x = 0; x < strlen(input) + 1; ++x)
{
if(input[x] == '\n')
{
input[x] = '\0';
}
}
if(strncmp(input, "exit", 4) == 0)
{
stillGoing = FALSE;
break;
}
num_of_inputs++;
cols = strlen(input) +1;
printf("maybe here (inputs realloc)\n");
temp = realloc(Inputs, num_of_inputs * sizeof(char *));
if(temp)
{
Inputs = temp;
Inputs[num_of_inputs - 1] = input;
}
else
{
/*handle error*/
printf("error\n");
exit(EXIT_FAILURE);
}
/*if(cols > old_cols)
{
for(i = 0; i < num_of_inputs; i++)
{
Inputs[i] = malloc(cols * sizeof(char));
}
old_cols = cols;
}*/
printf("maybe this is where it's seg faulted (strcpy)\n");
/* strcpy(Inputs[num_of_inputs - 1], input);*/
free(input);
input = NULL;
free(temp);
}
int z = 0;
for(z=0; i < num_of_inputs-1; z++)
{
printf("%s\n", Inputs[z]);
}
free(Inputs);
return 0;
}
I'm just wondering if you actually try anything anyone else posts, or just find new and interesting ways to screw up your own code.
Here, try this
Code:#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/wait.h>
typedef enum
{
FALSE = 0,
TRUE = 1
} Bool;
int main()
{
ssize_t readReturn = 0;
size_t bufLen = 0;
char **Inputs = NULL;
void *temp = NULL;
char *input = NULL;
Bool stillGoing = TRUE;
int num_of_inputs = 0;
int i = 0;
while(stillGoing)
{
printf("enter text: ");
fflush(stdout);
/* read a line, and blow away the newline */
readReturn = getline(&input, &bufLen, stdin);
if ( input[ strlen(input) - 1 ] == '\n' ) {
input[ strlen(input) - 1 ] = '\0';
}
/* time to quit yet? */
if(strncmp(input, "exit", 4) == 0)
{
stillGoing = FALSE;
break;
}
/* save the pointer getline() allocated for us */
num_of_inputs++;
temp = realloc(Inputs, num_of_inputs * sizeof(char *));
if(temp)
{
Inputs = temp;
Inputs[num_of_inputs - 1] = input;
}
else
{
/*handle error*/
printf("error\n");
exit(EXIT_FAILURE);
}
/* make getline call malloc again for the next line */
input = NULL;
}
/* print and free each line */
for(i=0; i < num_of_inputs-1; i++)
{
printf("%s\n", Inputs[i]);
free(Inputs[i]);
}
/* free all the pointers */
free(Inputs);
return 0;
}