0

I have something similar to this code in my application:

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

typedef struct STH
{
  double *buff;
  int size;
}STH;

void fun1(STH *s)
{
  fun3(s->buff, s->size);
}

void fun3(double *buff, int n)
{
  int i = 0;
  printf("N = %d\n", n);
  for(i=0; i<n; i++)
     printf("%d\n", buff[i]);
}

void fun2()
{
  STH s;

  s.size = 4;
  s.buff = malloc(sizeof(double) * s.size);

  fun1(&s);
}

int main()
{
    fun2();
    return 0;
}

When I try to print out the buffer in fun3, gdb says that there is an error in fun3. When I try to print out (in fun2) the value of the s->size, there is an error (instead of 4 it prints out strange numbers ....)

2
  • "%f\n" , s.buff = calloc(s.size, sizeof(double)) Commented Jan 5, 2014 at 18:51
  • fun3(..){..} before fun1(..){..} Commented Jan 5, 2014 at 18:54

1 Answer 1

1

buff[i] is a floating point number, use %f format specifier to print it. I.e. use:

printf("%f\n", buff[i]);

instead of

printf("%d\n", buff[i]);

Also you have not initialized your array to some values. It will contain (and print) random values.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.