Thread: Need help with calloc

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Need help with calloc

    Hello everyone I am working on a c program for a class and I am having issues with calloc. I am using calloc to create a dynamic array and then trying to store elements into that array after initialization. However, after I run my code everytime, I only have zeros in the array.

    Code:
    /* Chapter8Homework#3 */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    int main(void)
    {
        int x = 0, k = 0;
        float *array_size;
        printf("Please enter a number for the size of an array: ");
        scanf("%d", &x);
        array_size = (float *)calloc(x, sizeof(float));
        for(k=0;k<x;k++)
        {
            printf("Enter a number: ");
            scanf("%f", array_size + k);
        }
        for(k=0;k<x;k++)
        {
            printf("The number at element %d is %f\n", k, array_size + k);
        }
        return 0;
    }
    If anyone could help me out that would be much appreciated. I am really quite stuck because it seems to not make sense. Any help would be awesome. Thanks in advance!

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
    scanf("%f", &k);
    Your second scanf() should be this ^^

    Don't typecast malloc()/calloc() > FAQ > Casting malloc - Cprogramming.com

    Code:
    int x= 0, k = 0;
    /* ... */
    scanf("%d", &k);
    /* ... */
    for (k = 0; /* ... */ ) {
    No need to initialize them twice, both scanf() and the initial initialization of a for() loop take of that.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    Quote Originally Posted by memcpy View Post
    Code:
    scanf("%f", &k);
    Your second scanf() should be this ^^

    Don't typecast malloc()/calloc() > FAQ > Casting malloc - Cprogramming.com

    Code:
    int x= 0, k = 0;
    /* ... */
    scanf("%d", &k);
    /* ... */
    for (k = 0; /* ... */ ) {
    No need to initialize them twice, both scanf() and the initial initialization of a for() loop take of that.
    I think I understand what you are doing but if I were trying to use k as the counter to store whatever the user enters during that for loop into that array that I create with the calloc function would your code still be how I do that? I need the value entered into the scanf to be stored in array starting with the first element of which I thought was returned to me after I did the calloc call. Not sure if I am on the right track or not..

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by memcpy View Post
    Code:
    scanf("%f", &k);
    Your second scanf() should be this ^^
    No it shouldn't. k is the spot he's walking through. It's fine how he has it. The problem I have with his code is that he has x for a counter, and he calls the actual array array_size. That doesn't make any sense.

    The actual problem with his code is that he's printing the address of the spot in the array, not the value in the address:
    Code:
        printf("The number at element %d is %f\n", k, array_size + k);

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calloc
    By sukhdeep in forum C Programming
    Replies: 1
    Last Post: 12-17-2011, 02:59 AM
  2. calloc
    By dunsta in forum C Programming
    Replies: 5
    Last Post: 05-05-2010, 08:31 AM
  3. re-calloc???
    By audinue in forum C Programming
    Replies: 6
    Last Post: 07-20-2008, 05:06 PM
  4. Using calloc
    By giancu in forum C Programming
    Replies: 10
    Last Post: 11-07-2007, 01:32 PM
  5. Why use calloc()?
    By dwks in forum C Programming
    Replies: 8
    Last Post: 07-20-2005, 08:22 AM

Tags for this Thread