Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:15:12+00:00 2026-06-14T14:15:12+00:00

I have this code here that I think should work, and it does! except

  • 0

I have this code here that I think should work, and it does! except for the fact that though I’ve declared a pointer to an array of type double, it always stores int, and I don’t know why.

first, I have this struct and it is defined like this:

struct thing {
    int part1;
    double *part2;
};

then I initialize the thing by saying struct thing *abc = malloc (sizeof(struct thing)) and part1 = 0 and part2 = malloc(sizeof(double)).

then, I try to set specific values at specific positions in the array of double. This works fine with integers, but when I tried 0.5, it set the value to 0. when I tried 2.9, it set the value to 2. I really don’t know why it does this. code for setValue looks like this:

struct thing *setValue (struct thing *t, int pos, double set){
    if (t->part1 < pos){ // check to see if array is large enough
        t->part2 = realloc (t->part2, (pos+1) * sizeof(double));
        for (int a = t->part1 + 1; a < pos + 1; a++)
            t->part2[a] = 0;
    t->part1 = pos;
    }
    t->part2[pos] = set; // ALWAYS stores an integer, don't know why
    return t;
}

— Edit: So there is nothing really mallicious about this part; but here’s the rest of my code JUST IN CASE:

Relevant functions that operate on my struct thing

#include "thing.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

struct thing *makeThing(){ // GOOD
    struct thing *t = (struct thing *) malloc (sizeof(struct thing));
    t->part1 = 0;
    t->part2 = malloc (sizeof(double));
    t->part2[0] = 0;
    return t;
}

struct thing *setValue (struct thing *t, int pos, double set){
    if (t->part1 < pos){ // check to see if array is large enough
        t->part2 = realloc (t->part2, (pos+1) * sizeof(double));
        for (int a = t->part1 + 1; a < pos + 1; a++)
            t->part2[a] = 0;
    t->part1 = pos;
    }
    t->part2[pos] = set; // ALWAYS stores an integer, don't know why
    return t;
}

double getValue (struct thing *t, int pos){
    if (pos <= t->part1){
        return t->part2[pos];
    }
    return 0;
}

Header file:

#ifndef THING_H
#define THING_H

struct thing {
    int part1;
    double *part2;
};

struct thing *makeThing();
struct thing *setValue (struct thing *t, int pos, double set);
double getValue (struct thing *t, int pos);

#endif

main file:

#include <stdio.h>
#include "thing.h"

int main (void)
{
    struct thing *t = makeThing();
    setValue (t, 1, -1);
    setValue (t, 1, -2);
    setValue (t, 10, 1);
    setValue (t, 3, 1.5);

    printf ("%g\n", getValue (t, 0));
    printf ("%g\n", getValue (t, 1));
    printf ("%g\n", getValue (t, 2));
    printf ("%g\n", getValue (t, 3));
    printf ("%g\n", getValue (t, 4));
    printf ("%g\n", getValue (t, 5));
    printf ("%g\n", getValue (t, 6));
    printf ("%g\n", getValue (t, 7));
    printf ("%g\n", getValue (t, 8));
    printf ("%g\n", getValue (t, 9));
    printf ("%g\n", getValue (t, 10));

    return 0;
}

On my computer, this prints out:
0
-2
0
1
0
0
0
0
0
0
1

EDIT: Turns out that when I compile it via codeblocks, it works…

Ultimately, I am confused.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:15:13+00:00Added an answer on June 14, 2026 at 2:15 pm

    Double converts to int in C?

    No, it doesn’t. When you assign a double value to an object ot type double there is no conversion whatsoever.

    Your problem is not in the code you’ve shown; it is somewhere else (the way you print, some stupid #define, some other thing).

    Oh! And you really should make sure the realloc()s work. Otherwise, instead of an error, users may get a slightly erroneous value…

    As I said in a comment, your code works as expected at ideone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this html code that i want to edit with jQuery. Here is
I have no idea what this means. But here is the code that it
I have this code right here, where the $friends variable is an array with
I have this code here printf '$request1 = select * from whatever where this
I have this code here, which is intended to allow any type of arguments:
I have this code here, {foreach from=$cart.cartItems item=item name=cart} <div id=cart2Produkt> <p>{if $item.Product.ID} <a
I have this code here: var Person = (function() { var name; var PersonConstructor
So I have this code here: <table> <tr> <td width=200px valign=top> <div class=left_menu> <div
i have this quick issue please. I have this code here which permits me
I have this code down here. When format.js fires I want to serve to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.