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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:12:50+00:00 2026-05-18T08:12:50+00:00

Below is a Win32 Console App procedure that demonstrates the dependence of various pointers

  • 0

Below is a Win32 Console App procedure that demonstrates the dependence of various pointers on an array. A change to the values in the original array (model) by for example uncommenting the lines marked ‘// uncomment …’ results in a change to the output. My question is how do I get or mimic this behaviour in a C# managed code environment (i.e. without using unsafe and pointers)?

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    float model[100];
    for(int i = 0; i < 100; i++) { model[i] = i; }

    // uncomment these to alter the results
    //model[5] = 5000;
    //model[20] = 20000;
    //model[38] = 38000;

    static const int componentCount = 5;

    float* coefs = model;                   // coefs points to model[0]
    float* mean = coefs + componentCount;   // mean points to model[0 + componentCount] == model[5]
    float* cov = mean + 3*componentCount;   // cov points to model[0 + componentCount + 3*componentCount] == model[20] 

    int ci = 2;
    float* c = cov + 9*ci;  // c points to model[0 + componentCount + 3*componentCount + 9*ci] == model[38] 

    int i = 0;
    cout <<"model : "<< model[i] << endl;   // 0
    cout <<"coefs : "<< coefs[i] << endl;   // 0
    cout <<"mean  : "<< mean[i] << endl;    // 5 (or 5000)
    cout <<"cov   : "<< cov[i] << endl;     // 20 (or 20000)
    cout <<"ci    : "<< ci << endl;         // 2
    cout <<"c     : "<< c[i] << endl;       // 38 (or 38000)

cin.get(); }
  • 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-05-18T08:12:50+00:00Added an answer on May 18, 2026 at 8:12 am

    You can do the same thing in C# without unsafe code:

    struct ArrayPointer<T>
    {
        private T[] array;
        private int offset;
        public ArrayPointer(T[] array) : this(array, 0)
        {
        }
        private ArrayPointer(T[] array, int offset)
        {
            Debug.Assert(array != null);
            Debug.Assert(offset >= 0);
            Debug.Assert(offset < array.Length);
            this.array = array;
            this.offset = offset;
        }
        public static ArrayPointer<T> operator+(ArrayPointer<T> p1, int p2)
        {
            return new ArrayPointer<T>(p1.array, p1.offset + p2);
        }
    

    And so on. Define operators for addition, subtraction, increment, decrement, comparison, indexing, conversion from arrays, and so on. Then you can say:

    int[] arr = whatever;
    ArrayPointer<int> pointer = arr;
    pointer+=2;
    pointer--;
    int x = pointer[3];
    

    and so on.

    This approach has a lot of nice properties. For example, you can do a debug assert if you ever compare p1 > p2 when p1 and p2 are pointers to the interiors of different arrays. That is almost always a bug in C, but a hard one to catch.

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

Sidebar

Related Questions

Compile error in vs2010(Win32 Console Application Template) for the code below. How can I
I am writing a windows forms app that can launch a console for debugging.
I have the windows console program code below that should show up NT Authoroty\Network
I have code that is using a win32 api from a web app. I
Below is code to an inherited ComboBox. The issue is that the ComboBox is
I created win32 console application in vs2010 (without select the option of precompiled header).
I run my C code in vs2010 (win32 console application). It was compiled as
I'm trying to build a simple form in a c++ win32 console application. instead
I have a win32 program that creates a bitmap screenshot. I am trying to
I have developed one console application and use WMI query as per below queryCollection

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.