2 Replies - 384 Views - Last Post: 08 March 2011 - 05:43 PM Rate Topic: -----

#1 lyndsayh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-March 11

Writing a table in C but having a problem understanding how to use poi

Posted 08 March 2011 - 04:56 PM

Hi, I'm programming a code to generate a table of values with an operator and have to use pointers. I'm having a problem understanding how pointers work and how to use them in the function, even after reading chapters on the topic. If someone could help me out by maybe giving a brief explanation, it would be of great help.
Is This A Good Question/Topic? 0
  • +

Replies To: Writing a table in C but having a problem understanding how to use poi

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,433
  • Joined: 25-December 09

Re: Writing a table in C but having a problem understanding how to use poi

Posted 08 March 2011 - 05:02 PM

You may want to study this link.

If you have questions about the information in the link then ask.

If you understand this page then you should try to create a small program using the knowledge gained.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Writing a table in C but having a problem understanding how to use poi

Posted 08 March 2011 - 05:43 PM

Quote

If someone could help me out by maybe giving a brief explanation, it would be of great help.


A pointer is a variable that holds a memory address. This is very useful because a pointer can "refers to" or "point to" other data. To really make this useful you need to be able to "de-reference" the data, that is access the data at the memory address pointed to.

So:

int var = 10; // var is some data *somewhere* in memory
int* ptr = &var; // &var returns the "address of" var and so now ptr is a pointer to var.

*ptr = 2; //*ptr dereferences the address ptr and gives access to the data there

cout << var << endl; // prints 2


So to access different locations in memory we can assign a value to a pointer and then dereference the pointer to access (read/write) the data at that memory location!

Example: Strings in C are a collection a chars with the last one equal to 0. Generally a pointer is used to point to the first char in the string.
#include <iostream>

using namespace std;

int main() {
    const char* aString = "I had a tiny turtle his name was Tiny Tim.";
    
    cout << "Address of aString's data in memory = " << (void*)aString << endl;
    cout << "value of aString's data in memory = \"" << aString << "\"" << endl;
    cout << "ADDRESS : ch : ASCII" << endl;
    for (const char* ptr = aString; *ptr !=0; ptr++) {
        cout << (void*)ptr << ": " << *ptr << "  : " << hex << (int)*ptr << endl;
    }

    return 0;
}
OUTPUT:
> "C:\CProjects\Forum Help\ptrExample1.exe " 
Address of aString's data in memory = 00E89334
value of aString's data in memory = "I had a tiny turtle his name was Tiny Tim."
ADDRESS : ch : ASCII
00E89334: I  : 49
00E89335:    : 20
00E89336: h  : 68
00E89337: a  : 61
00E89338: d  : 64
00E89339:    : 20
00E8933A: a  : 61
00E8933B:    : 20
00E8933C: t  : 74
00E8933D: i  : 69
00E8933E: n  : 6e
00E8933F: y  : 79
00E89340:    : 20
00E89341: t  : 74
00E89342: u  : 75
00E89343: r  : 72
00E89344: t  : 74
00E89345: l  : 6c
00E89346: e  : 65
00E89347:    : 20
00E89348: h  : 68
00E89349: i  : 69
00E8934A: s  : 73
00E8934B:    : 20
00E8934C: n  : 6e
00E8934D: a  : 61
00E8934E: m  : 6d
00E8934F: e  : 65
00E89350:    : 20
00E89351: w  : 77
00E89352: a  : 61
00E89353: s  : 73
00E89354:    : 20
00E89355: T  : 54
00E89356: i  : 69
00E89357: n  : 6e
00E89358: y  : 79
00E89359:    : 20
00E8935A: T  : 54
00E8935B: i  : 69
00E8935C: m  : 6d
00E8935D: .  : 2e

> Process Exit Code: 0
> Time Taken: 00:00


So as I dereference each address I find that there is a printable ASCII char there and so this data is some text, not all data is text data though. Strings in C rely heavily upon pointers to access blocks of continuous memory containing text data. Rather than requireing a variable for each letter, I can refer to the address of the first character and mark the ending of the string with a '/0' char.

Part II: pointers know data size

Pointers are addresses and all addresses are is numbers. But pointers are kind of special because the know the "size" of the type they refer to in memory. So a char is typically 1 byte so when you add 1 to a char* the address goes up by 1 to get you to the next char. But an int is typically 32bits or 4 bytes so when you add 1 to an int* the address goes up by 4 bytes to get you to the next int in memory.

#include <iostream>

using namespace std;

int main() {
    int piDigits[] = {3, 1, 4, 1, 5, 9, 2, 6, 5};
        
    cout << "Address of piDigits's data in memory = " << piDigits << endl;
    
    cout << "ADDRESS : digit" << endl;
    for (int* ptr = piDigits; *ptr < sizeof(piDigits)/sizeof(*piDigits); ptr++) {
        cout << (void*)ptr << ": " << *ptr << endl;
    }

    return 0;
}
OUTPUT:
> "C:\CProjects\Forum Help\ptrExample2.exe " 
Address of piDigits's data in memory = 0026F98C
ADDRESS : digit
0026F98C: 3
0026F990: 1
0026F994: 4
0026F998: 1
0026F99C: 5

> Process Exit Code: 0
> Time Taken: 00:00


note the addresses go up by 4: 0026F98C + 4 = 26F990

So when you add/subtract a number to a pointer it slides the address up/down by units of the size of that pointer's base type. char 1byte, short 2 bytes, int 4 bytes (these values are platform dependent).

So if we have int* ptr = 0x800000; (some random address) then ptr = ptr + 10 will be 0x800028 -- since 10*4 = 40 = 0x28

so the array access operator ptr[5] is the same as *(ptr + 5) -- which means that rather than doing a lot of pointer arrithmatic to access elements of a block of memory like a string we can use the array access operator [] to do it!

I hope this helps, sorry it was not as brief as I wanted.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1