help ith understanding -> pointer

Anddos

Hunter
Joined
Oct 3, 2003
Messages
68
Reaction score
0
sometimes ive seen -> used alot in function parameters..
example
void hit(class1 *class1,class2 *class2)
{
class1->member; //member is just something made up.
class2->member; //same as above
}

now if i was going to using this example function
in main() ,
how is it going to work
hit(class1 *class1,class2 *class2); //because it wont let me do this
so i dont think i have the grasp of -> properly

thanks for the help
 
So, your question doesn't make a whole lot of sense, but here goes:

A pointer is a representation of an address in RAM. -> is a convenient pointer dereference in C/C++. Saying a->b is the same as saying (*a).b, it's just takes less effort to type.

I don't know what you mean by "it won't let me do this." If that's what you're trying to compile, verbatim, then I think you probably have a bit to learn before you need to worry yourself with pointers...

Anyway, here's some example code, which I always find more useful than English... It does what I think you were trying to do. It's in C, and based on the use of your variable names "class1" and "class2" I think you're probably trying to learn C++. (Of course, this code is also valid C++.) The reason I wrote it for you in C is that I think you should probably get the hang of dynamic memory and structs before you learn how to handle objects.

Code:
//example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H

// the struct is the important thing here
// typedef just lets us refer to something later by a more convenient name
struct classOne;
typedef struct ClassOne class1;
struct classOne {
  int member;
};

// an equivalent declaration for class2
typedef struct classTwo {
  int member;
}class2;

void hit(class1* class1, class2* class2);

void reallyHit(class1* class1, class2* class2);

#endif//EXAMPLE_H

Code:
//example.c
#include <stdlib.h>
#include <stdio.h>
#include "example.h"

void hit(class1* class1, class2* class2) {
  class1->member;
  class2->member;
  return;
}

void reallyHit(class1* class1, class2* class2) {
  class1->member++;
  class2->member++;
  return;
}

int main (int argc, char* argv[]) {
  class1* one = (class1*) malloc(sizeof(class1));//ask for some dynamic memory
  class2* two = (class2*) malloc(sizeof(class2));
  printf("Initializing structs...\n");
  one->member = 0;//initialize newly allocated memory
  two->member = 0;
  
  printf("Accessing \"one->member\"\t\t%d\n", one->member);
  printf("Accessing \"(*one).member\"\t%d\n", (*one).member);
  
  printf("The address of \"one\" is %x...\n", one);
  printf("...and the address of \"one->member is\" %x\n", &(one->member));
  
  printf("Hitting...\n");
  hit(one, two);//call a procedure that does nothing
  printf("one's member:\t%d\ntwo's member:\t%d\n", one->member, two->member);

  printf("Really hitting...\n");
  reallyHit(one, two);//call a procedure that barely does anything
  printf("one's member:\t%d\ntwo's member:\t%d\n", one->member, two->member);

  return 0;
}
 
Back
Top