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 9252313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:54:49+00:00 2026-06-18T10:54:49+00:00

I am using functions from a library where a most-important function takes arguments of

  • 0

I am using functions from a library where a most-important function takes arguments of type const short int*. What I have instead is int * and was wondering if there was a way of casting an int * into a const short int*. The following code snippet highlights the problem I am facing:

/* simple program to convert int* to const short* */


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

    void disp(const short* arr, int len) {

      int i = 0;
      for(i = 0; i < len; i++) {
        printf("ith index = %hd\n", arr[i]);
      }
    }

    int main(int argc, char* argv[]) {

      int len = 10, i = 0;
      int *arr = (int *) malloc(sizeof(int) * len);

      for(i = 0; i < len; i++) {
        arr[i] = i;
      }

      disp(arr, len);

      return 0;
    }

The above code snippet compiles.
This is what I have tried so far:
1. Tried the c-style cast. Function call looked something like so:
disp((const short*) arr, len). The resulting output was quite weird:

ith index = 0
ith index = 0
ith index = 1
ith index = 0
ith index = 2
ith index = 0
ith index = 3
ith index = 0
ith index = 4
ith index = 0 
  1. Tried the const-ness cast. Function call looked like:
    disp(const_cast<const short*> arr, len);
    This resulted in an error while compiling.

My question(s):
1. Why is the output in approach 1 so weird? What is going on over there?
2. I saw some examples that remove const-ness using the const cast in approach 2. I do not know how to add the same.
3. Is there a way to cast an int* into a const short int*?

P.S: If there are questions of this sort that have been asked before, please do let me know. I googled it and did not find anything specific.

  • 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-18T10:54:50+00:00Added an answer on June 18, 2026 at 10:54 am

    Right, so the SIZE of a short is different to an int in many environments (it doesn’t have to be, and there are certainly compilers that for example have 16-bit int and 16-bit short).

    So what you get out of casting a pointer in this way is, a) undefined behaviour, and b) unless you know exactly what you are doing, probably not what you wanted anyway.

    The output from your code looks perfectly fine from what I’d expect, so clearly you are not telling us something about what you are trying to do!

    Edit: Note that since pointers are “size-dependant”, if you cast a pointer of one size to a pointer to a type of a different size, the alignment of the data will be wrong, which is why every other value is zero in your output – because an int is 4 bytes [typically] and a short is 2 bytes, the pointer will “step” 2 bytes for each index, (so arr[i] will be original arr + i * 2 bytes. where the int * arr has a ‘step’ of 4 bytes, so arr + i * 4 bytes. Depending on your integer values, you will get some “half an integer” values out of this – since your numbers are small, this is zero.

    So, whilst the compiler is doing exactly what you ask it to do: make a pointer to short point to a lump of memory containing int, it won’t do what you EXPECT it to do, namely translate each int into a short. To do that, you will either have to do:

    short **sarr = malloc(sizof(short *) * 10); 
    
    for(i = 0; i < 10; i++)
    {
        sarr[i] = (short *)(&arr[i]);       // This may well not work, since you get the "wrong half" of the int.
    }
    

    If this gives the wrong half, you can do this:

         sarr[i] = (short *)(&arr[i])+1;      // Get second half of int.
    

    But it depends on the “byte-order” (big endian or little endian) so it’s not portable. And it’s very bad coding style to do this in general code.

    Or:
    short *sarr = malloc(sizof(short *) * 10);

    for(i = 0; i < 10; i++)
    {
        sarr[i] = (short)(arr[i]);
    }
    

    The second method works in the sense that it makes a copy of your integer array into a short array. This also doesn’t depend on the order the content is stored, or anything like that. Of course, if the value in arr[i] is greater than what can fit in a short, you don’t get the same value as arr[i] in your short!

    And in both cases don’t forget to free your array when it is no longer needed.

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

Sidebar

Related Questions

Using the MSXML2 functions from the msxml3.dll library, I'm trying to duplicate sections in
i have a problem using some php functions on a string i get from
I'm using the most recent PI-OLEDB library to read data from aggregate views in
I'm using various functions from the earthdistance module in PostgreSQL, one of those being
I'm using two functions one is from this site http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/ and its using function
I am creating screenshots under Windows and using the LockBits function from GDI+ to
I call a javascript function from a textbox by using OnKeyPress=clickSearchButton() Here is my
I am using JNI to call my C++ function from Java. One of the
I would be calling procedure or function from java using JDBC . In terms
hello i am using this Page.ClientScript.RegisterStartupScript() to call a javascript function from vb code

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.