Pointers to arrays

This is a discussion on Pointers to arrays within the C++ Programming forums, part of the General Programming Boards category; Hey, im working with c++ for some time. And i wonder how to send there pointer and use in function. ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    69

    Pointers to arrays

    Hey, im working with c++ for some time. And i wonder how to send there pointer and use in function.

    In delphi i could do something like this :

    Code:
    procedure func(var a:Integer);
    begin
       inc(a,10);
    end;
    
    ...
    
    a := 10;
    func(a);
    // and here i got a = 20
    In c++ i noticed it works and u don't need to type 'var' (btw i can be dangerous, when i forget to not manipulate on varaible). But it's not work for me when i want do it by function which i have declared in class saved as header. That's why i need pointer to array.

    I wonder how to access pointer in that function. Cause i tried like this :

    *somevariable[0] = 5;
    *somevariable[0] = 2;
    But it changed address of point i guess, beacuse when i checked this array it was like : 0x05000000(...)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    It's hard to say, when your example is for an integer, yet you mention arrays (without context) for your C++.

    One thing to bear in mind though, depending on what you're doing is that
    *somevariable[0] = 5;
    should be written as
    (*somevariable)[0] = 5;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by kargo View Post
    In c++ i noticed it works and u don't need to type 'var' (btw i can be dangerous, when i forget to not manipulate on varaible). But it's not work for me when i want do it by function which i have declared in class saved as header. That's why i need pointer to array.

    I wonder how to access pointer in that function.
    I am a bit confused what you want to achieve. Post some pascal code. And no, in c++ it does not work, you still need to add something (the & operator).
    I never put signature, but I decided to make an exception.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kargo View Post
    Hey, im working with c++ for some time. And i wonder how to send there pointer and use in function.

    In delphi i could do something like this :

    Code:
    procedure func(var a:Integer);
    begin
       inc(a,10);
    end;
    
    a := 10;
    func(a);
    // and here i got a = 20
    The equivalent in C or C++ would be:
    Code:
    void func(int *a)
      { 
         *a += 10; 
      }
    
    
    int b =10;
    func ( &b);
    The trick is to work with pointers in the function but hand it the address of the variable.

    (I was a Pascal programmer for a long time, until Borland killed it...)
    Last edited by CommonTater; 03-12-2011 at 02:42 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    A better example that works in C++ would be:
    Code:
    void func(int& a)
    {
    	a += 10; 
    }
    
    int b = 10;
    func(b);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with arrays, pointers, and structs.
    By RexInTheCity in forum C Programming
    Replies: 5
    Last Post: 03-29-2010, 04:30 PM
  2. Pointers As 2D arrays
    By TieFighter in forum C Programming
    Replies: 29
    Last Post: 03-22-2010, 07:46 AM
  3. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  4. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 03:31 PM
  5. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21