c - string manipulation to struct member - c

Based on my previous post, I came up with the following code. I'm sure there is a better way of doing it. I'm wondering, what would that be?
It does split the string if greater than max chars OR if # is found. Any ideas would be appreciated!
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct my_struct {
char *str;
};
int main () {
struct my_struct *struc;
int max = 5;
char *tmp = "Hello World#Foo Bar In here#Bar Foo dot com#here#there";
struc = malloc (20 * sizeof (struct my_struct));
int strIdx = 0, offSet = 0;
char *p = tmp;
char *tmpChar = malloc (strlen (tmp) + 1), *save;
save = tmpChar;
while (*p != '\0') {
if (offSet < max) {
offSet++;
if (*p == '#') {
if (offSet != 1) {
*tmpChar = '\0';
struc[strIdx++].str = strndup (save, max);
save = tmpChar;
}
offSet = 0;
} else
*tmpChar++ = *p;
} else { // max
offSet = 0;
*tmpChar = '\0';
struc[strIdx++].str = strndup (save, max);
save = tmpChar;
continue;
}
p++;
}
struc[strIdx++].str = strndup (save, max); // last 'save'
for (strIdx = 0; strIdx < 11; strIdx++)
printf ("%s\n", struc[strIdx].str);
for (strIdx = 0; strIdx < 11; strIdx++)
free (struc[strIdx].str);
free (struc);
return 0;
}
Output at 5 chars max:
Hello
Worl
d
Foo B
ar In
here
Bar F
oo do
t com
here
there

Alright, I'll take a crack at it. First, let me say that my formatting changes were for me. If you don't like lonely {s, that's fine.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct string_bin
{
char *str;
};
int main ()
{
struct string_bin *strings;
char *tmp = "Hello World#Foo Bar In here#Bar Foo dot com#here#there";
char *p = tmp;
strings = malloc (20 * sizeof (struct string_bin));
memset(strings, 0, 20 * sizeof (struct string_bin));
int strIdx = 0, offset = 0;
char *cursor, *save;
strings[strIdx].str = malloc(MAX+1);
save = strings[strIdx].str;
while (*p != '\0')
{
if (offset < MAX && *p != '#')
{
*(save++) = *(p++);
offset++;
continue;
}
else if (*p == '#')
*p++;
offset = 0;
*save = '\0';
strings[++strIdx].str = malloc(MAX+1);
save = strings[strIdx].str;
}
*save = '\0';
for (strIdx = 0; strings[strIdx].str != NULL; strIdx++)
{
printf ("%s\n", strings[strIdx].str);
free (strings[strIdx].str);
}
free (strings);
return 0;
}
The big change is that I got rid of your strdup calls. Instead, I stuffed the string directly into its destination buffer. I also made more calls to malloc for individual string buffers. That lets you not know the length of the input string ahead of time at the cost of a few extra allocations.

Related

Why is the integer in the struct a garbage values even though it was changed?

So, I implemented split in C, now I know strtok exists, but I wanted to implement it, so my function returns a struct that has the string array and the length, which is decided by the number of times the delimiter occurs and whether it's the first value or not, here's the code.
split.h
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int count_delm(char *, char);
char *make_str(char);
struct split_output {
char **arr;
int size;
};
typedef struct split_output split_arr;
split_arr *split(char *, char);
split.c
#include "split.h"
int count_delm(char *str, char delm) {
register int lpvar = 0;
register int counter = 0;
while (str[lpvar] != '\0') {
if (str[lpvar] == delm) {
counter++;
}
lpvar++;
}
return counter;
}
char *make_str(char ch) {
char *ret_str = (char *)malloc(2);
ret_str[0] = ch;
ret_str[1] = '\0';
return ret_str;
}
split_arr *split(char *str, char delm) {
int num_delm = count_delm(str, delm);
char **final_arr;
register int lpvar = 0;
register int arr_counter = 0;
char *concat_str = (char *)malloc(2);
int ret_size = 0;
if (str[0] == delm) {
concat_str = make_str(str[1]);
final_arr = (char **)malloc(sizeof(char *) * (num_delm));
ret_size = num_delm;
lpvar++;
} else {
concat_str = make_str(str[0]);
final_arr = (char **)malloc(sizeof(char *) * (num_delm + 1));
ret_size = num_delm + 1;
}
while (1) {
if (str[lpvar + 1] != '\0') {
if (str[lpvar + 1] != delm) {
concat_str = strcat(concat_str, make_str(str[lpvar + 1]));
lpvar++;
} else {
final_arr[arr_counter] = concat_str;
arr_counter++;
if (str[lpvar + 2] != '\0') {
lpvar++;
lpvar++;
concat_str = make_str(str[lpvar]);
}
}
} else {
arr_counter++;
final_arr[arr_counter] = concat_str;
printf("%s is the last at pos %d\n", concat_str, arr_counter);
break;
}
}
split_arr *ret_struct = (split_arr *)malloc(sizeof(split_arr));
(*ret_struct).size = ret_size;
(*ret_struct).arr = final_arr;
return ret_struct;
}
That was the code of the split implementation, here's the code that tests it.
test.c
#include "split.h"
int main() {
char *x = "lryabruahsdfads";
split_arr output = *(split(x, 'a'));
char **read = output.arr;
int len = output.size;
int loop = 0;
printf("size: %d", len - 1);
for (loop = 0; loop < len; loop++) {
puts(*(read + loop));
}
return 0;
}
Here's the output when executed:
ds is the last at pos 4
size: 3lry
bru
hsdf
Segmentation fault (core dumped)
Why is output.size 3lry? I de-referenced the struct pointer to get the struct so there's nothing wrong with that. I can't find the error, I've been trying to debug for almost an hour.
In your split.c file over here:
else{
arr_counter++; //comment this line
final_arr[arr_counter]=concat_str;
printf("%s is the last at pos %d\n",concat_str,arr_counter);
break;
}
You are incrementing variable arr_counter which you should not because you already incremented it inside the if. Just comment out this line and your code works fine.
And your output is fine just add a line return to the printf statements like:
printf("size: %d \n",len-1);

C Reallocation of 2d array function

My reallocation does not work (Segmentation fault for 11.element), I would like to enlarge the array twice, the length of the column is constant according to the first input.I would like to allocate in function.
In the function vztvorPole I allocate an array of 10 rows and x columns.
char vytvorPole(char ***grid, int nrows, int ncols)
{
*grid = malloc( sizeof(*grid)*nrows);
if (*grid == NULL)
{
printf("ERROR\n");
return 1;
}
for(int i=0;i<nrows;i++)
{
(*grid)[i]=(char *) malloc (ncols*sizeof(*grid));
if((*grid)[i] == NULL)
{
printf("ERROR\n");
return 1;
}
}
return 0;
}
char realokuj(char ***grid, int nrows, int ncols)
{
char **docasne;
docasne = (char**)realloc (*grid, nrows*sizeof(*grid));
for(int i=nrows/2;i<nrows;i++)
{
(docasne)[i]=(char *) malloc (ncols*sizeof(*grid));
}
*grid = docasne;
}
int main (void)
{
char **diagonaly;
int rDiagonaly = 10;
int cDiagonaly = -1;
char *str = NULL;
size_t capacity = 0;
int first = 1;
int nr = 0;
printf("Vypln:\n");
while ( getline (&str, &capacity, stdin) != -1)
{
if(str[0] == '\n')
break;
if (first)
{
cDiagonaly = strlen (str);
vytvorPole(&diagonaly, rDiagonaly, cDiagonaly);
first = 0;
}
if (nr==rDiagonaly)
{
rDiagonaly *= 2;
realokuj(&diagonaly, rDiagonaly, cDiagonaly);
}
strcpy(diagonaly[nr],str);
nr++;
}
}
Your code is a bit more complicated than needed.
If you initialize diagnonaly (e.g. char **diagnoaly = NULL;), you can eliminate vytvorPole and just use realokuj. That is, if you also do: int rDiagonaly = 0;
That's because realloc will handle a NULL pointer just fine (i.e. it's equivalent to malloc in that case).
And, you don't need to do:
(docasne)[i]=(char *) malloc (ncols*sizeof(*grid));
in the subfunction.
It's better to use strdup in main. If we do that, we don't need cDiagonaly at all. And, we can have strings of varying lengths.
Passing a triple star pointer (e.g. char ***grid) has issues. You could just pass it as char ** and use return to update the value since you don't really use the existing return.
But, this can really be done from main without a separate function.
While it may be possible to fix your existing code, it really should be simplified:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
#if 0
char **diagonaly;
int rDiagonaly = 10;
#else
char **grid = NULL;
int rmax = 0;
#endif
#if 0
int cDiagonaly = -1;
#endif
char *str = NULL;
size_t capacity = 0;
#if 0
int first = 1;
#endif
int nr = 0;
printf("Vypln:\n");
while (getline(&str,&capacity,stdin) != -1) {
if (str[0] == '\n')
break;
if (nr == rmax) {
rmax += 100;
grid = realloc(grid,sizeof(*grid) * (rmax + 1));
if (grid == NULL) {
perror("realloc");
exit(1);
}
// this is similar to what you did, but it's not necessary because
// of "grid[nr] = NULL;" below
#if 0
for (int idx = nr; idx < rmax; ++idx)
grid[idx] = NULL;
#endif
}
grid[nr++] = strdup(str);
grid[nr] = NULL;
}
return 0;
}
Here's the cleaned up version [without the cpp conditionals]:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
char **grid = NULL;
int rmax = 0;
char *str = NULL;
size_t capacity = 0;
int nr = 0;
printf("Vypln:\n");
while (getline(&str,&capacity,stdin) != -1) {
if (str[0] == '\n')
break;
if (nr == rmax) {
rmax += 100;
grid = realloc(grid,sizeof(*grid) * (rmax + 1));
if (grid == NULL) {
perror("realloc");
exit(1);
}
}
grid[nr++] = strdup(str);
grid[nr] = NULL;
}
return 0;
}
Is something like that okay?
while ( getline (&str, &capacity, stdin) != -1)
{
if(str[0] == '\n')
break;
if (first)
{
cDiagonaly = strlen (str);
diagonaly = (char**) malloc(sizeof(*diagonaly)*rDiagonaly);
if (nr==rDiagonaly)
{
rDiagonaly *= 2;
docasne = (char**)realloc (diagonaly, rDiagonaly*sizeof(*diagonaly));
diagonaly=docasne;
}
diagonaly[nr]=(char*) malloc (cDiagonaly*sizeof(**diagonaly));
strcpy(diagonaly[nr],str);
nr++;
}

Function returns address of local variable, in c

Keep getting an error on my return ret before the main () class (end of process request)
buddy.c: In function `process_request':
buddy.c:89: warning: function returns address of local variable
Error I receive , what I'm trying to do is print the results I get from my process_request to my print near the end of my main() function, help?
//used a flag
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define F_SIZE 2
#define A_SIZE 2
#define BUDDY_SIZE 4*1024 // in bytes
// compile using gcc-o buddy buddy.c -lm
// block information
struct block_info
{
char AF_flag; // flag
int data; // data in the block
};
typedef struct block_info block;
block buddy_block[BUDDY_SIZE]; // entire buddy system to be used in this array
int block_count = 0; // number of blocks in buddy_block
int get_block_size(int num)
{
int i = 0;
for (i = 0; num < pow(2.0, (double)i); ++i);
return (int)(pow(2.0, (double)i));
}
char *process_request(char *s, int len)
{
block b;
block n;
int i, j, count, block_size = 0;
int first_buddy_size = 0;
int second_buddy_size = 0;
char ret[BUDDY_SIZE] = { 0 };
char *response[BUDDY_SIZE] = { 0 };
if (!s)
return NULL;
first_buddy_size = buddy_block[0].data;
second_buddy_size = buddy_block[1].data;
block_size = get_block_size(atoi(s));
// get the matching FREE block in the power of 2
if (*s == 'A')
{ // Allocation request
int i = 0;
char *buff = NULL;
// split the block
char strf[F_SIZE] = { 0 };
char stra[A_SIZE] = { 0 };
strf[0] = 'F';
stra[0] = 'A';
for (i = 0; block_size <= first_buddy_size / 2; ++i)
{
first_buddy_size /= 2;
sprintf(buff, "%d", first_buddy_size);
response[i] = strcat(strf, buff);
}
sprintf(buff, "%d", block_size);
response[i] = strcat(stra, buff);
// update the array
count = i;
for (i = 0, j = count; j; --j, ++i)
{
char *str = response[j];
buddy_block[i].AF_flag = *str++;
while (*str)
buddy_block[i].data = *str;
}
}
else if (*s == 'F')
{ // Free request
for (i = 1; i < block_count; ++i)
{ // traversing through the array
if (buddy_block[i].data = block_size)
{ // b.AF_flag = 'B';
i << 1;
}
}
}
// update array
count = i;
for (i = 0, j = count; j; --j, ++i)
{
char *str = response[j];
buddy_block[i].AF_flag = *str++;
while (*str)
buddy_block[i].data = *str;
}
return ret; // ------------error: warning functions returns address
// of local variable----------
}
int main(int argc)
{
block t;
int i;
char ch;
char *ret = NULL;
char line[20];
t.AF_flag = 'X'; // some junk means memory block not even accessed
t.data = 0;
for (i = 0; i < BUDDY_SIZE; i++)
buddy_block[i] = t; // initialize with 0 bytes and no information about
// Allocation/Free
// initially there is only one Free block of 4K bytes
t.AF_flag = 'F';
t.data = BUDDY_SIZE;
buddy_block[0] = t; // started the buddy block to 4096 bytes, all free to be
// allocated
++block_count;
while (1)
{
// get user input
char request[5] = { 0 }; // 'F4096' or 'A4096', max 5 chars
int correct_input = 0;
char ch;
for (i = 0, ch = 'X'; ch != '\n'; ++i)
{
ch = getchar();
if ((i == 0) && (ch != 'A' || ch != 'F'))
{
printf("Illegal token!!! : should be A or F");
correct_input = 0;
break;
}
if (ch < '0' && ch > '9')
{ // illegal code
printf("Illegal token!!! : should be 0 and 9");
}
correct_input = 1;
request[i] = ch;
}
if (correct_input)
{
// process user input
ret = process_request(request, sizeof(request));
printf("%d", ret); // [512](512A)(128A)(128F)(256F)(1024F)(2048F)
// //fprintf(stderr, "I am in stderr");
fflush(stdout);
}
}
return 0;
}
You have allocated ret on the stack. Although it is not forbidden to return an address to that the stack will be reused by any function that is called afterwards thus overwriting whatever was at that address.
You may want to consider moving this data onto the caller's stack or into dynamic memory.
char * foo() {
char string[] = "Hello world\n";
return string;
}
int main () {
printf("%s", foo());
}
Will most likely not print "Hello World!".
One right way would be:
void foo(char * buffer) {
memcpy(buffer, "Hello world\n", sizeof("Hello world\n"));
}
int main () {
char buffer[100];
foo(&buffer);
printf("%s", buffer);
}
Or with dynamic memory (prone to memory leaks):
char * foo() {
char * string = malloc(sizeof("Hello world\n"));
memcpy(string, "Hello world\n", sizeof("Hello world\n"));
return string;
}
int main () {
char * string = foo();
printf("%s", string);
free(string);
}
It means exactly what it says. You are doing
char* process_request(char*s, int len) {
...
char ret[BUDDY_SIZE] = {0};
...
return ret;
}
ret is an address to a memory location. The issue is that such memory location points to a local variable. A local variable lies in the stack, and its memory may be (probably will) reused for other variables when you call new functions.
To avoid that, return a pointer to a memory location that has been dynamically allocated (that means malloc and friends).
You are returning a local pointer from a function and that is an undefined value.
char ret[BUDDY_SIZE] = {0};
SO, your compiler is throwing that error. Assign your pointer dynamically and the error should go away.

printf overwriting seeminlgy unrelated data?

EDIT: I should add how I have this all set up. The struct definition and prototypes are in mystring.h. The function definitions are in mystring.c. The main is in mystringtest.c. For mystring.c and mystringtest.c, I have #include "mystring.h" at the top. I'm compiling like gcc -o test.exe mystring.c mystringtest.c. Not sure if any of that matters, but I'm new with C so I'm just trying to include everything.
I have a good deal of experience with Java but am pretty new to C. I imagine this is related to pointers and memory but I'm totally at a loss here for what's going on. Here's my code:
typedef struct {
char *chars;
int length;
int maxSize;
} String;
int main() {
char *a;
a = readline();
String *s = newString(a);
int b = length(s);
printf("length is %d \n", b);
}
I run the program and enter "hello" (as prompted by readline()). I've stepped through the program and after length(s), s->chars is still a pointer to the array of chars 'hello'. After the print statement, s->chars becomes a pointer to the array of chars 'Length is %d \n'. I'm totally at a loss for what I'm doing wrong. I'm working on a virtual machine if that matters at all. Any help is greatly appreciated. I'll give the code for newString and length too.
int length(String *s) {
char *temp = s->chars;
char b = *temp;
int count;
if (b == '\0') { count = 0; }
else { count = 1; }
while (b != '\0') {
b = *(temp+count);
count++;
}
return count;
}
String *newString(char *s) {
String st;
st.length = 20;
st.maxSize = MAXCHAR;
char *temp = malloc(20 * sizeof(char));
char b = *s;
int count = 0;
while (b != '\0') {
*(temp + count) = b;
count++;
b = *(s+count);
if (count == st.maxSize) { break; }
if (count == st.length) {
st.length = st.length + 20;
temp = realloc(temp, st.length * sizeof(char));
}
}
st.chars = temp;
return &st;
}
String *newString(char *s) {
String st;
...
return &st;
}
You are returning a pointer to a local variable. After newString returns, the local variable no longer exists, so you have a dangling pointer.
Either allocate st with malloc, or return it by value.
you must null terminate the string after the while loop, you have not left space for the null terminator. Also I don't see why you need to realloc
//using strlen will eliminate the need for realloc, +1 is for the null terminator
int len = strlen(s)
char *temp = malloc((len * sizeof(char)) +1);
//null terminate
*(temp+count) = '\0';
st.chars = temp;

string array in c

How do you use a string array as a parameter in C? If I were to write a function with signature:
Guess i didnt explain myself very well... I'll post the code that i'm trying to get to work.
int format_parameters(char* str) {
char local_str[201] = "";
int i = 0;
int j = 0;
int flip = 0;
while(str[i]) {
if((str[i] == '"') && (flip == 0)) flip = 1;//Sentence allowed
else if((str[i] == '"') && (flip == 1)) flip = 0;//Sentence not allowed
if(flip == 1) append_char(local_str, str[i]);
//check if space
else if(flip == 0) {
int c = str[i];
if(!isspace(c)) append_char(local_str, str[i]);
else {
if((strlen(local_str) > 0) && (j < 4)) {
//local-str copied to param[j] here
//printf("j = %d %s\n",j,local_str);
local_str[0] = '\0';
j++;
}
}
}
i++;
}
//Add \0 to param
return flip;
}//end format_parameters
void append_char(char* str, char c) {
int len = strlen(str);
str[len] = c;
str[len+1] = '\0';
}//end append_char
int main() {
char str[200];
//str filled with stuff...
int x = format_parameters(str);
}
There should be a second (and third?) parameter in format_parameterssignature, a char* param[5] which should be readable from main.
Does this work?
#include <string.h>
#include <assert.h>
#include <stdio.h>
int format_parameters(char *str, char *param[], size_t nparam)
{
char **next = param;
char **end = param + nparam;
char *data = str;
assert(str != 0 && param != 0 && nparam != 0);
while (next < end && *data != '\0')
{
*next++ = data;
data = strchr(data, ' '); // Choose your own splitting criterion
if (data == 0)
break;
*data++ = '\0';
}
return(next - param);
}
int main(void)
{
char str[] = "a b c d";
char *param[5];
int nvals = format_parameters(str, param, 5);
int i;
for (i = 0; i < nvals; i++)
printf("Param %d: <<%s>>\n", i+1, param[i]);
return 0;
}
The return value is the number of parameters found. If you pass an empty string, that would be 0. Beware leading, trailing and repeated blanks; the code works - but maybe not as you want it to.
This is entirely about memory allocation.
If you allocate static memory for param before the function is called, the memory will exist in that scope.
Otherwise take a look at dynamic allocation, it will exist until you tell it to go away.
You have to create the char* param[] array outside the function and just pass it as a parameter:
int paramCount = countParameters(str); // you have to create this function
char* param[] = malloc(paramCount * sizeof(char*));
format_parameters(str, param);
and inside the function:
int format_parameters(char* str, char* param[])
{
int currentParamIndex = 0;
..........
//TODO: check if currentParamIndex < paramCount
char* currentParam = str + currentParamStart; // currentParamStart is the start index of the parameter in the str containing all parameters
param[currentParamIndex] = currentParam;
currentParamIndex++;
.............
}
And in order to write safe code you have to pass also the paramCount as a parameter to format_parameters so the function will not access an element out of the bounds of the array.
Or maybe you should just use getopt?
As Jonatahan pointed out, you need more parameters:
int format_parameters(char* strInput, char* paramOutput[], size_t cbMaxParams );
// return value is actual number of parameter strings in paramOutput
paramOutput is an array of pointers. So the caller has to provide an array of pointers and the called function has to allocate memory for the strings and set the pointers in the array:
// main:
#define SIZE 20
char * params[SIZE];
int result = format_parameters( szInput, params, SIZE );
// after use go through params and free all pointers
// function:
int format_parameters(char* strInput, char* paramOutput[], size_t cbMaxParams )
{
// ...
for( size_t i=0; (i<cbMaxParams) && (!noMoreParams); i++ )
{
// ...
paramOutput[i] = (char *)malloc( xxxx );
// ...
}
// ...
}

Resources