Results 1 to 7 of 7

Thread: Pointer usage?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517

    Red face Pointer usage?

    I was reading, and I never did understand pointers...
    But I think I get the purpose now...

    Say we have a object that takes 2kb of ram, and to pass
    that object to a routine would mean copying that 2kb to
    execute that routitine!

    But with a point its 2bytes of memory to pass... Just the
    address of the object... Right?

    So the purpose of a pointer is to save time by just passing the
    address?

    Is that all correct?

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Pointers (in the sense you mentioned) is just like passing ByRef in VB.

    C++ has two ways to pass parameters - by value (the default)
    or by reference (using a pointer).

    BTW an array reference is a pointer already.

    Pointers are used a lot for processing data as well. They are just not in VB, so their use will seem weird to someone who only knows VB, I guess.

    For example, find the character furthest along in the collating (ASCII) sequence in a string:

    Code:
    unsigned char maxchar(char *s) // an array of char passed as a pointer
    {    
     unsigned char *buf;            // working pointer
     char retval='\0';                  // char to return default is null
     buf=(unsigned char*) s;     // work against s using buf
     while (*buf!='\0'){              // loop til end of string
          if( (*buf >  retval ) 
                           retval=buf;   // if bigger, replace value of retval
          buf++;                           // next character in the array
     }
     return retval;                     // return biggest one found
    }

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    But is the point of using them to speed of Routine calls or not?

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Only in a small part - using them like in the code above, or using them to generalize (you can create function pointers) programming is what they are all about.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Originally posted by jim mcnamara
    Only in a small part - using them like in the code above, or using them to generalize (you can create function pointers) programming is what they are all about.
    I dont understand..

    What asspect of your program will it improve in the way your specified?

  6. #6
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    The example uses a pointer like Mid(a$,x,1) in Visual Basic.
    YOu use it for lists, arrays, queues, so on. You cannot do that in VB. You also use pointers to create data storage.

    You can use a pointer like:
    Code:
    'vb
    for x = 1 to 100
      b=a(x)
    next
    ' C++
    int *a=&b[0];
    for(i=0;i<100;i++) b=*a++;
    What you need to do is mess around with about 4-5 different programs.

  7. #7
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    linked lists are excellent with pointers. They will, like you said, use less memory on your computer. I'm not sure how much extra SPEED you'll get, but the memory can really be saved like that.
    If I agree with you today, don't get used to it.

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