Results 1 to 4 of 4

Thread: [RESOLVED] pointer newbie question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Resolved [RESOLVED] pointer newbie question

    Hello, I am new to C#, and I came across a tutorial about pointer:

    int *x ;

    Declares a pointer variable x, which can hold the address of an int type. The reference operator (&) can be used to get the memory address of a variable.

    int x = 100;

    The &x gives the memory address of the variable x, which we can assign to a pointer variable

    I don't understand what that "address of" mean?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: pointer newbie question

    The address of an object is the location in memory at which it resides. Each byte in memory has a numerical address the address of an object is the address of the first byte in memory it occupies. A pointer is a variable that contains the memory address of an object. If you have a non-pointer variable you can use the "&" operator to its address, which can then be assigned to a pointer variable.

    If you're new to C# then I don't think you need be worrying about unsafe code and pointers just yet, unless you are already a relatively advanced programmer. The .NET Framework is constructed in such a way that you don't need pointers in the vast majority of situations. There are some situations where using pointers can dramatically speed up computationally-intensive sections of code, but for the most part they are unnecessary.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: pointer newbie question

    just to expand on that - think of a method that you create. You pass it a value... let's say x, normally you are passing by value, meaning that you pass a copy of the variable x into your method, when you do whatever computation if you don't return that value when the execution is complete then the value of x isn't changed. however if you where to pass by reference it is like saying "there is a variable stored at this memory location" if you then "follow the pointer" and do some computation then the value of x is changed in that case.

    as was said before - C# doesn't really need pointers for most of what the world is doing with it. I avoid them, that is probably good advice.

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [RESOLVED] pointer newbie question

    Pointers can make it much easier to use the API and also interface with binaries created in other programming languages (C/C++, ASM, Delphi...).
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width