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

  • Home
  • SEARCH
  • 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 7759517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:39:01+00:00 2026-06-01T13:39:01+00:00

The following C++ Win32 console program assigns an array to a pointer to void,

  • 0

The following C++ Win32 console program assigns an array to a pointer to void, and prints the results in two different ways:

// Foo.cpp : A Win32 console application.
//
#include "stdafx.h"

typedef unsigned char elem_type;
#define ELEM_COUNT 4

int _tmain(int argc, _TCHAR* argv[])
{
    elem_type *ary = new elem_type[ELEM_COUNT];
    for (int i = 0; i < ELEM_COUNT; i++)
    {
        ary[i] = ((i + 1) * 5); // multiples of 5
    }
    void *void_ary = ary;

    for (int i = 0; i < ELEM_COUNT; i++)
    {
        printf("void_ary[%d] is %u\t", i, ((elem_type*)void_ary)[i]);
        printf("*(void_ary+%d) is %u\n", i, *((elem_type*)(void_ary))+i);
    }

    void *allocd_ary;
    return 0;
}

Here is the output:

void_ary[0] is 5        *(void_ary+0) is 5
void_ary[1] is 10       *(void_ary+1) is 6
void_ary[2] is 15       *(void_ary+2) is 7
void_ary[3] is 20       *(void_ary+3) is 8

Using square brackets prints the result that I expected. But dereferencing pointer offsets does not, even though the array is being typecast.

Why the discrepancy?

  • 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-01T13:39:03+00:00Added an answer on June 1, 2026 at 1:39 pm

    Well that is because you are dereferencing the value then adding ‘i’ to the result. You need some more parenthesis around the pointer cast or using static_cast which is more obvious.

    As in:

    *(static_cast<elem_type*>(void_ary)+i)

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

Sidebar

Related Questions

I'm writing a (Win32 Console) program that wraps another process; it takes parameters as
Consider the following C program: #include <stdio.h> #include <stdarg.h> typedef void callptr(); static void
I have a third-party (Win32) DLL, written in C, that exposes the following interface:
Following is the script for matching regex and storing value in array: sub b1
When developing a Win32 Application (non-console application) in Visual Studio 2005, is there any
I would like to have the ability to process Win32 messages in a console
I have a header-file, the COM interface. I have created a small win32 program
My goal is to create a simple Win32 Console application that uses HunSpell to
I have a DLL with the following code using System.Text; using Microsoft.Win32; using System.Diagnostics;
I created a Win32 Console application, and hide the cmd window. So if my

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.