First let me start saying I have read all the questions related to this subject and can't find a solution for my problem. All the answers seem to be to stop using pointers and chars(which I need to do) or are about other structures.
I need to create a function that returns an integer, but also saves the steps of the process, which I will save as characters using snprintf and pointers. I will leave the basic idea but it's way more complicated than what I will put.
#include <stdio.h>
#include <string.h>
int sum_and_diff (int a, int b, int *res, char *text);
int main(void){
int b = 2;
int diff;
char texto;
printf("La suma de 5 y %d es %d\n", b, sum_and_diff(5, b, &diff, &texto));
printf("La diferencia de 5 y %d es %d\n", b, diff);
printf("El texto es %s\n", texto);
}
int sum_and_diff (int a, int b, int *res, char *text){
char texto;
int sum;
//text = malloc(sizeof(char) * 254);
//text = (char *)malloc(sizeof(char) * 254);
sum = a + b;
*res = a - b;
texto = "Hola";
strcpy(*text, texto);
//strcpy(text, texto);
return sum;
}
I wanted to use this example because it shows how you can use pointers inside of a function to get more information, the same procedure used to get diff is not working for the character type.
The only difference with my actual program is that the variable "texto" gets its character value from snprintf (does what I need it to do, I have checked by priting the variable inside the function). The problem is in getting the pointer to point to the variable.
Thanks! I am using gcc 4.9 to compile if it makes a difference. The things that are commented are things I have tried that haven't worked, have also tried some other slight variations from those.