3

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.

M.O.
  • 476
  • 7
  • 19
  • 3
    Hint: `char texto;` is not what you want. – Sourav Ghosh Nov 16 '16 at 14:25
  • and what do I want? – M.O. Nov 16 '16 at 14:27
  • `texto = "Hola";`...you tell me? :) – Sourav Ghosh Nov 16 '16 at 14:27
  • A little star... ;) – LPs Nov 16 '16 at 14:27
  • Out of interest, could you use C++, where such things are easier? – Bathsheba Nov 16 '16 at 14:27
  • see [this](http://stackoverflow.com/a/40631662/2173917), specifically #2 – Sourav Ghosh Nov 16 '16 at 14:28
  • 3
    @Bathsheba don't go over to the dark side. ;) – LPs Nov 16 '16 at 14:28
  • Sourav Ghosh I didn't put it like that because in my actual program I define first the variable `texto`, and then I give it its value way down the program with snprintf, wanted it to look as much as the program as possible. Bathsheba, I can't use C++, it's for a class where I am supposed to learn C. – M.O. Nov 16 '16 at 14:30
  • I initialized the variable in my big program like `char texto[128]; memset(texto , 0, sizeof(texto));` and then changed its value with snprintf as the program went along. So I dont think #2 applies even though to the actual thing even though it could to the example. – M.O. Nov 16 '16 at 14:32
  • 1
    Possible duplicate of [How to correctly assign a new string value?](http://stackoverflow.com/questions/3131319/how-to-correctly-assign-a-new-string-value) – LPs Nov 16 '16 at 14:34
  • Think about , a char and a collection of chars :) There must be some difference in the usage. – Akash Mahapatra Nov 16 '16 at 14:34
  • LPs, I had already seen that thread, it does not resolve my question since they are working with struct and not pointers. – M.O. Nov 16 '16 at 14:36
  • It uses arrays of chars, sturct is only a container in that case. You should take a look to [this post](http://stackoverflow.com/questions/1461432/what-is-array-decaying) too – LPs Nov 16 '16 at 14:40
  • 2
    Heed the compiler warnings, the compiler is your friend. – MathiasE Nov 16 '16 at 14:41
  • 2
    [example of modify](http://ideone.com/YhC2WR) – BLUEPIXY Nov 16 '16 at 14:42
  • LPs, I am not sure how what says in the struct thread can help with pointers, or how the last thread you put could help either, really cool information though. Could you be more explicit in how you think it would help? I really dont see any code that could be used for my example, but then again I haven't been at it for long. – M.O. Nov 16 '16 at 14:46
  • Thanks BLUEPIXY! It works! Do you know why you dont have to add the & before the variable? – M.O. Nov 16 '16 at 14:50
  • 1
    Arrays are treated as pointers when passing to functions. – BLUEPIXY Nov 16 '16 at 14:53
  • 1
    [example if you want allocate by malloc at inside function.](http://ideone.com/YhC2WR) – BLUEPIXY Nov 16 '16 at 15:01
  • Thanks BLUEPIXY. You could write your answer again below so that I can close this thread as solved :) – M.O. Nov 16 '16 at 15:09

1 Answers1

-1

Declaring a pointer to char without allocation is a mistake. You see my code. [c]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE    8

int sum_and_diff (int a, int b, int *res, char *text);

int main(void)
{
    int b = 2;
    int diff;
    char *texto = (char *)malloc(SIZE);
    if (!texto)
        abort();
    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);
    free(texto);
}

int sum_and_diff (int a, int b, int *res, char *text)
{
    char *texto = (char *)malloc(SIZE);
    int sum;
    if (!texto)
        abort();
    sum = a + b;
    *res = a - b;
    texto = "Hola";
    strcpy(text, texto);
    free(texto);
    return sum;
}
Chung Lim
  • 169
  • 1
  • 1
  • 10
  • Now would be a good time to demonstrate the importance of free() – schil227 Nov 16 '16 at 15:58
  • free() is very important as C doesn't have garbage collection feature. How can I forget that? Hmm... – Chung Lim Nov 16 '16 at 16:36
  • Curious why cast `(char *)` in `char *texto = (char *)malloc(SIZE);` versus just simply `char *texto = malloc(SIZE);`? Certainly `char *texto = malloc(SIZE);` is legal C. – chux - Reinstate Monica Nov 16 '16 at 22:25
  • For sake of compatibility with C++, we use char *texto = (char *)malloc(SIZE). – Chung Lim Nov 17 '16 at 03:53
  • You could use `char texto[SIZE];` inside the function (or in both cases really) – M.M Nov 17 '16 at 04:03
  • 1
    `texto = "Hola;"` leaks memory and then `free(texto)` is undefined behaviour. `=` is different to `strcpy`. – M.M Nov 17 '16 at 04:04
  • Why is it necessary to use free(texto), thought that since sum_and_diff is a function on it's own the memory used by it would be deleted on its own. – M.O. Dec 04 '16 at 20:06