0
Good afternoon.
I am in doubt whether it is possible to pass values to a C struct using JSON, Hibernate or some other known method?
0
Good afternoon.
I am in doubt whether it is possible to pass values to a C struct using JSON, Hibernate or some other known method?
2
Oh guys, I managed to solve the problem, I’ll leave the code in case someone needs one of these days
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct __attribute__((__packed__)){
char *s;
int *x;
}my_struct;
void *chr2struct(const char *text){
char c;
my_struct *mst = (my_struct *) malloc(sizeof(my_struct));
void **swap = (void **) malloc(sizeof(my_struct));
void **res = swap;
if(!text)
return 0;
int i;
while(*text){
c = *text;
text++;
char str[32];
int x, len;
char *data;
switch(c){
case 's':
case 'c':
sscanf(text,"%[^\037]",str);
text += strlen(str)+1;
len = atoi(str);
data = (void *) malloc(sizeof(char)*len);
strcpy(data,text);
text+= len+1;
swap[i] = data;
break;
case 'i':
sscanf(text,"%[^\037]",str);
text += strlen(str)+1;
len = atoi(str);
data = (void *) malloc(sizeof(int)*len);
for(x=0; x<len; x++){
sscanf(text,"%i",&data[x]);
sprintf(str,"%i",data[x]);
text+=strlen(str)+1;
swap[i] = *data;
}
break;
default:
return 0;
}
i++;
}
return res;
}
int main(int argc, char *argv[]){
my_struct *mst;
mst = (my_struct *) chr2struct("s16\037qwertyuiopasdfgh\0i1\03716\0");
printf("%s | %i\n", mst->s, mst->x);
}
If you are learning C, I advise you to use a C compiler instead of a C++ compiler. Learning C using a C++ compiler is like learning Swedish with a Danish teacher.
Turn on all your compiler warnings and pay attention to warnings!
I program directly from the linux terminal, I don’t use ide, as for the compiler I use gcc even, and the warnings, only had one in sscanf that reads an integer, but at the moment I don’t intend to create an entire variable to receive the integer values.
Hmmm, I didn’t notice the <malloc.h>
. Anyway you must prefer the <stdlib.h>
(that I noticed does not exist in the code) and fail to make the conversation to the value returned by malloc()
.
Browser other questions tagged c struct
You are not signed in. Login or sign up in order to post.
Pointer Reaf/write (like char*) is typically nontextual. and you’re writing a memory address... Writing and reading pointers, could only work within the same process (within the same session)
– JJoao
What is the purpose? Otherwise the answer will be too broad.
– krystalgamer
I want to pass the values I collect from the database to a C struct
– Brumazzi DB
So for the record - this is called "serialization" (in this case, to read back, "deserialization")
– jsbueno
You noticed that there is no "automated" way to do this - you had to create a mini-protocol for serialization - in your case, mark the string and integer fields with a special character. There are thousands of different serialization protocols in the world - and depending on the application, it may be worth adopting a more standardized one, so that other programs can understand your data.
– jsbueno