"*Program received signal SIGSEGV, Segmentation fault.
0x000000000040073b in concat (nextstr=0x7fffffffee66 "the",
longstr=0x7fffffffe750 "", i=0x0) at main.c:24
24 printf("%c -> %c\n", nextstr[j], longstr[*i]);
(gdb)"
// define max for command line
#define MAX_CHARS 1000
#include <stdio.h>
int concat(char[], char[], int *);
void printreverse(char[], int);
int main(int argc, char *argv[])
{
char longstring[MAX_CHARS] = { '\0' };
int i = 0, j;
if (argc < 2)
{
printf("%s requires command-line args\n", argv[0]);
return 1;
}
for (j = 1; j <= argc; j++)
{
if (concat(argv[j], longstring, i))
return 1;
}
i--;
longstring[i] = '\0'; //delete trailing space
printf("%s\n", longstring);
}
int concat(char nextstr[], char longstr[], int *i)
{
int j = 0;
while (nextstr[j] != '\0')
{
printf("%c -> %c\n", nextstr[j], longstr[*i]);
longstr[*i] = nextstr[j];
(*i)++;
j++;
if (*i > MAX_CHARS)
{
printf("Error: Input is too long!\n");
return 1;
}
}
if (j > 0)
{
if ((*i) + 2 > MAX_CHARS)
{
printf("Error: Input is too long!\n");
return 1;
}
longstr[*i] = ' ';
longstr[(*i) + 1] = '\0';
(*i)++;
}
return 0;
}
Pointer error most likely
"This takes a command line argument is checks the length but there's a seg fault"
I think the error is on line 33
but I can't figure out how to fix it
The main problem that is causing the crash is in your for loop:
for(j = 1; j <= argc; j++){
if(concat(argv[j], longstring, i)) return 1;
}
Since concat's third parameter is an int *, you need to pass a pointer to i. Also, you are iterating through the loop one too many times. If argc is 2, then the program name is in argv[0], and your argument is in argv[1]. So, change the loop to this:
for(j = 1; j < argc; j++) {
if (concat(argv[j], longstring, &i))
return 1;
}
Related
I'm having troubling figuring out why not my code is not iterating through the if statement until it finds the correct character.
#include <stddef.h>
#include <stdio.h>
char * my_strstr(char * param_1, char * param_2) {
int i;
int j;
for (i = 0; param_1[i] != '\0'; i++) {
printf("this is a loop for j %c\n", param_1[i]);
for (j = 0; param_2[j] != '\0'; j++) {
printf("this is a loop for j %c\n", param_2[j]);
if (param_1[i] == param_2[j]) {
printf("figuring out this %c\n", param_1[i]);
j++;
i++;
return ¶m_1[i];
printf("figuring out this %c\n", param_1[i]);
}
}
}
}
int main() {
char * param_1 = "hello";
char * param_2 = "ello";
printf("This is the string %s \n", my_strstr(param_1, param_2));
return 0;
}
I've pasted your code below with some edits and comments I hope will help.
#include <stddef.h>
#include <stdio.h>
char * my_strstr(char * param_1, char * param_2) {
int i;
int j;
for (i = 0; param_1[i] != '\0'; i++) {
// this should say "loop for i" shouldn't it?
printf("this is a loop for j %c\n", param_1[i]);
// use a flag to determine if you have found a match or not
int found = 1;
for (j = 0; param_2[j] != '\0'; j++) {
/* you actually want to compare param_1[i + j] == param_2[j] here
(respecting bounds of course). */
if (! param_1[i + j] || param_1[i + j] != param_2[j]) {
found = 0;
break;
}
printf("this is a loop for j %c\n", param_2[j]);
/* i don't understand what the following code was meant to do but you will
rarely want to increment i and j outside of the for ()
if (param_1[i] == param_2[j]) {
printf("figuring out this %c\n", param_1[i]);
j++;
i++;
return ¶m_1[i];
printf("figuring out this %c\n", param_1[i]);
}
*/
}
// here you want to check if you found it, and return if so.
if (found) {
return ¶m_1[i];
}
}
// need a default for if it is never found:
return NULL;
}
int main() {
char * param_1 = "hello";
char * param_2 = "ello";
printf("This is the string %s \n", my_strstr(param_1, param_2));
return 0;
}
Sort command of linux must sort the lines of a text file and transfer the output to another file. But my code gives a runtime error. Please rectify the pointer mistakes so that output.
In which line exactly should I make changes? Because there is no output after all.
I'm pasting the whole code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sortfile(char **arr, int linecount) {
int i, j;
char t[500];
for (i = 1; i < linecount; i++) {
for (j = 1; j < linecount; j++) {
if (strcmp(arr[j - 1], arr[j]) > 0) {
strcpy(t, arr[j - 1]);
strcpy(arr[j - 1], arr[j]);
strcpy(arr[j], t);
}
}
}
}
int main() {
FILE *fileIN, *fileOUT;
fileIN = fopen("test1.txt", "r");
unsigned long int linecount = 0;
int c;
if (fileIN == NULL) {
fclose(fileIN);
return 0;
}
while ((c = fgetc(fileIN)) != EOF) {
if (c == '\n')
linecount++;
}
printf("line count=%d", linecount);
char *arr[linecount];
char singleline[500];
int i = 0;
while (fgets(singleline, 500, fileIN) != NULL) {
arr[i] = (char*)malloc(500);
strcpy(arr[i], singleline);
i++;
}
sortfile(arr, linecount);
for (i = 0; i < linecount; i++) {
printf("%s\n", arr[i]);
}
fileOUT = fopen("out.txt", "w");
if (!fileOUT) {
exit(-1);
}
for (i = 0; i < linecount; i++) {
fprintf(fileOUT, "%s", arr[i]);
}
fclose(fileIN);
fclose(fileOUT);
}
The problem in your code is you do not rewind the input stream after reading it the first time to count the number of newlines. You should add rewind(fileIN); before the next loop.
Note however that there are other problems in this code:
the number of newline characters may be less than the number of successful calls to fgets(): lines longer than 499 bytes will be silently broken in multiple chunks, causing more items to be read by fgets() than newlines. Also the last line might not end with a newline. Just count the number of successful calls to fgets().
You allocate 500 bytes for each line, which is potentially very wasteful. Use strdup() to allocate only the necessary size.
Swapping the lines in the sort routine should be done by swapping the pointers, not copying the contents.
allocating arr with malloc is safer and more portable than defining it as a variable sized array with char *arr[linecount];
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sortfile(char **arr, int linecount) {
for (;;) {
int swapped = 0;
for (int j = 1; j < linecount; j++) {
if (strcmp(arr[j - 1], arr[j]) > 0) {
char *t = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = t;
swapped = 1;
}
}
if (swapped == 0)
break;
}
}
int main() {
FILE *fileIN, *fileOUT;
char singleline[500];
int i, linecount;
fileIN = fopen("test1.txt", "r");
if (fileIN == NULL) {
fprintf(stderr, "cannot open %s\n", "test1.txt");
return 1;
}
linecount = 0;
while (fgets(singleline, 500, fileIN)) {
linecount++;
}
printf("line count=%d\n", linecount);
char **arr = malloc(sizeof(*arr) * linecount);
if (arr == NULL) {
fprintf(stderr, "memory allocation failure\n");
return 1;
}
rewind(fileIN);
for (i = 0; i < linecount && fgets(singleline, 500, fileIN) != NULL; i++) {
arr[i] = strdup(singleline);
if (arr[i] == NULL) {
fprintf(stderr, "memory allocation failure\n");
return 1;
}
}
fclose(fileIN);
if (i != linecount) {
fprintf(stderr, "line count mismatch: i=%d, lilnecount=%d\n",
i, linecount);
linecount = i;
}
sortfile(arr, linecount);
for (i = 0; i < linecount; i++) {
printf("%s", arr[i]);
}
fileOUT = fopen("out.txt", "w");
if (!fileOUT) {
fprintf(stderr, "cannot open %s\n", "out.txt");
return 1;
}
for (i = 0; i < linecount; i++) {
fprintf(fileOUT, "%s", arr[i]);
}
fclose(fileOUT);
for (i = 0; i < linecount; i++) {
free(arr[i]);
}
free(arr);
return 0;
}
To get a different sort order, you would change the comparison function. Instead of strcmp() you could use this:
#include <ctype.h>
int my_strcmp(const char *s1, const char *s2) {
/* compare strings lexicographically but swap lower and uppercase letters */
unsigned char c, d;
while ((c = *s1++) == (d = *s2++)) {
if (c == '\0')
return 0; /* string are equal */
}
/* transpose case of c */
if (islower(c)) {
c = toupper(c);
} else {
c = tolower(c);
}
/* transpose case of d */
if (islower(d)) {
d = toupper(d);
} else {
d = tolower(d);
}
/* on ASCII systems, we should still have c != d */
/* return comparison result */
if (c <= d)
return -1;
} else {
return 1;
}
}
So I'm writing a somewhat simple C program that is supposed to take a string of characters separated by semicolons as input. The program is then supposed to sort the strings by length and print them to the console.
Ex: abc;12;def;1234
The issue I'm having is that any numbers that are entered end up being printed as random symbols and I'm not sure why. I'm taking in input in this function:
void get_strings(char** c)
{
while (scanf("%[^;]s", c[numStrings]) != EOF)
{
getchar();
numStrings += 1;
}
}
Since scanf is looking for strings, if numbers are entered, are they stored as the 'character form' of those numbers, or should I be casting somehow?
Here's the rest of the code:
int numStrings = 0;
void sort_strings(char** c)
{
for (int i = 0; i < numStrings; i++)
{
for (int j = 0; j < numStrings - i; j++)
{
if (strlen(c[j]) > strlen(c[j + 1]))
{
char temp[1000];
strcpy(c[j], temp);
strcpy(c[j + 1], c[j]);
strcpy(temp, c[j + 1]);
}
}
}
}
void show_strings(char** c)
{
for (int i = 0; i < numStrings; i++)
{
if (printf("%s\n", c[i]) != EOF) break;
}
}
int main()
{
char wordLen[100][1000];
char* word2[100];
for (int i = 0; i < 100; i++)
{
word2[i] = wordLen[i];
}
char** words = word2;
get_strings(words);
sort_strings(words);
show_strings(words);
return 0;
}
The parsing code is incorrect:
void get_strings(char **c) {
while (scanf("%[^;]s", c[numStrings]) != EOF) {
getchar();
numStrings += 1;
}
}
the scanf() format contains an extra s that does not match the input.
the return value of scanf() should be compared to 1 to ensure successful conversion. Conversion failure produces EOF only at end of file, otherwise it produces 0 and the contents of c[numStrings] will be indeterminate.
conversion stops at the first character ;, this character stays in the input stream, but it is read by getchar(), yet if there is an empty field, the corresponding conversion would fail and the contents of the array would be indeterminate.
you should not use a global variable for the number of strings read. You should instead return this number.
The sorting code is incorrect too:
the inner loop runs one index too far: j + 1 must be less than numStrings for all runs.
the arguments to strcpy are passed in the wrong order.
strcpy should not be used at all, you should just swap the pointers.
show_strings() always stops after the first line as printf will return the number of characters printed.
You can fix the reading loop this way:
#include <stdio.h>
#include <string.h>
int get_strings(char **c, int maxStrings) {
int numStrings = 0;
while (numStrings < maxStrings) {
switch (scanf("%999[^;]", c[numStrings])) {
case 1:
getchar();
numStrings += 1;
break;
case 0:
if (getchar() == ';') {
c[numStrings] = '\0';
numStrings += 1;
}
break;
case EOF:
return numStrings;
}
}
}
void sort_strings(char **c, int count) {
for (int i = 0; i < count; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (strlen(c[j]) > strlen(c[j + 1])) {
char *temp = c[j];
c[j] = c[j + 1];
c[j + 1] = temp;
}
}
}
}
void show_strings(char **c, int count) {
for (int i = 0; i < count; i++) {
printf("%s\n", c[i]);
}
}
int main(void) {
char words[1000][100];
char *wordPtrs[100];
int numStrings;
for (int i = 0; i < 100; i++) {
wordPtrs[i] = words[i];
}
numStrings = get_strings(wordPtrs, 100);
sort_strings(wordPtrs, numStrings);
show_strings(wordPtrs, numStrings);
return 0;
}
I have a problem with an "add calculator".
Valgrind reports no memory errors, no errors from compiler but the program doesn't show any output despite the printf - "Base is ".
All pointers, and variables are (n my opinion) correctly initialized.
getnum function gets a number, returns a pointer to char - char *,
add function processes two numbers as strings, returns result which is a pointer to char (char *) as well.
I don't know whether the problem is memory allocation or procedures connected to processing arrays...
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX(A,B) ((A)>(B) ? (A) : (B))
char *getnum(FILE *infile, int base)
{
int len = 10;
int c;
int pos = 0;
char *num = NULL;
char *tmpnum = NULL;
num = malloc(sizeof(char)*len);
while (((c = fgetc(infile)) != EOF) && (isalnum(c))) {
if (isdigit(c)) {
/* irrelevant*/
}
else if (isalpha(c)) {
fprintf(stderr, "Wrong base, expected 16\n");
free(num);
return NULL;
}
if (pos >= len) {
/*realloc*/
}
}
return num;
}
int main(int argc, char **argv)
{
FILE *infile = NULL;
char *number1 = NULL;
char *number2 = NULL;
char *result = NULL;
int base, i, j = 0, length, count = 0;
infile = fopen(argv[1], "r");
base = atoi(argv[2]);
while (!feof(infile)) {
number1 = getnum(infile, base);
number2 = getnum(infile, base);
break;
}
printf("Base is %d\n", base);
result = add(number1, number2, base);
length = strlen(result);
for (i = 0; i <= length - 1; i++) {
if (result[i] == '0') {
count++;
}
}
for (j = i; j == (length - 1); j++) {
printf("Result is: %s\n", &result[j]);
break;
}
free(result);
result = NULL;
fclose(infile);
return 0;
}
Trying to work it out for the past 4 hours and can't find a mistake.
Thanks in advance!
There is one severe typo near the end of main().
for (j = i; j == (length - 1); j++) {
/* ^^ SHOULD BE <= */
printf("Result is: %s\n", &result[j]);
break;
}
Looking at this code:
for (i = 0; i <= length - 1; i++) {
if (result[i] == '0') {
count++;
}
}
if (count == length) {
printf("Result is 0\n");
free(result);
result = NULL; /* arguable */
fclose(infile);
return 0;
}
for (i = 0; i <= length - 1; i++) {
if (result[i] != '0') {
break;
}
}
for (j = i; j == (length - 1); j++) {
printf("Result is: %s\n", &result[j]);
break;
}
Instead of counting the total number of zeroes in the output number, and then counting the number of leading zeroes again, why not combine the two?
What is the last loop about? It's not even really a loop - it will execute once if i is length - 1, or not at all if not (presumably you're hitting the latter case in your test input).
e.g.
for (count = 0; count < length; count++) {
if (result[count] != '0')
break;
}
if (count == length) {
printf("Result is 0\n");
free(result);
result = NULL; /* arguable */
fclose(infile);
return 0;
}
printf("Result is: %s\n", &result[count]);
How can I concatenate all the values in argv to one string using snprintf?
if i pass in values like ./prog val1 val2 val3 val4
my string
char all_values[MAX_LEN] should be "val1 val2 val3 val4"
How can I do this efficiently using snprintf()?
#include <stdio.h>
#define MAX_LEN 16
int main(int ac, char **av) {
char buffer[MAX_LEN];
buffer[0] = 0;
int offset = 0;
while(av++,--ac) {
int toWrite = MAX_LEN-offset;
int written = snprintf(buffer+offset, toWrite, "%s ", *av);
if(toWrite < written) {
break;
}
offset += written;
}
printf("%s\n", buffer);
}
If you to want make a print of N arguments, you can do
int i = 1 ; // first parameter is a program name
while(i < argc )
{
printf("%s",argv[1]);
i++;
}
But if you want to use a string in other processor,you would really concatenate then. Maybe with:
char* string_result;
int i = 1;
int size_total = 0;
bool space_needed = false;
while(i < argc) { // argc contain the number of arguments
size_total += strlen(argv[i])+1; //+1 for a new space each time.
i++;
}
if(i > 2) {
space_needed = true;
size_total -= 1; //no need for space at end of string
}
string_result = (char*)malloc((size_total+1)*sizeof(char));
string_result[0] = 0 ; // redundant?
i = 1;
while(i < argc) {
strcat(string_result,argv[i]); // caution to concatenate argv string, memory of OS.
if(space_needed && (i+1) < argc)
strcat(string_result, " "); //space so it looks better.
i++;
}
//free pointer when done using it.
free(string_result);
Assuming sizoeof(char)==1, untested code!
#include <stdlib.h>
#include <string.h>
int i ;
int size_total = 0;
size_t *lens=(size_t *)malloc((argc)*sizeof(size_t));
for (i=1;i < argc; i++) {
lens[i]=strlen(argv[i]);
size_total += lens[i]+1;
}
concatinated = (char*)malloc(size_total);
char *start=concatinated;
for (i=1;i < argc; i++) {
memcpy(start, argv[i], lens[i]);
start+=lens[i];
*start=' ';
start++;
}
start--;
*start=0;
free(lens);