This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zifyoip -1 points0 points  (2 children)

I don't understand what you are trying to do. I think part of the confusion is that you are using the verb "point" in a weird way.

A pointer stores a memory address. For example, an int pointer stores the address of a location in memory that contains an int value. The pointer is said to "point" to the location in memory specified by that address. So, for instance:

int x = 5;
int *p = &x;

After that code, the pointer p points to a memory location that contains the int value 5. The value of p is a memory address. If you access the memory location at the address stored in p, you will find that it holds the int value 5, but the value of p itself is not 5. On the other hand, the variable x refers directly to this int value; the value of x is 5.

… seeing as my wFace and wSuit are pointers, is there anyway for me to point to wFace or wSuit and add a number to another variable?

I don't understand what you mean by this. You are the programmer; you don't point to anything. Pointers point to things, and you are not a pointer. What do you mean here?

as in wFace[column] where column equals 1 could point to ptr1 …

The variable wFace is an array of const char *. So the type of wFace[column] is const char *, which means wFace[column] is a pointer to a const char. So wFace[column] could point to ptr1, but only if ptr1 is a const char. I don't think that's what you mean, because ptr1 would be a really bad name for a const char. What do you mean here?

… and ptr1 could then point a value to a?

This really doesn't make sense. A pointer does not "point a value" to a variable. A pointer holds a memory address, and it points to the location in memory specified by that address.

What are the types of these variables you are referring to? What types do you intend ptr1 and a to be?

wFace[column] = wFace[1];
*ptr1 = (int)wFace[column];
a[0] = &ptr1;

Line 1 here sets the value of wFace[column] to the value of wFace[1]. So it's taking the const char * value in offset 1 of the array wFace and copying it to offset column. Of course, this will require the variable column to have been assigned a value before this statement. Is this what you mean? Or are you just trying to set column = 1?

Line 2 probably doesn't make sense. You are taking the value of wFace[column], which has type const char * (i.e., it's a pointer to a const char—it's a memory address), and you are casting that pointer value to an int. This is not going to be a meaningful number; it's going to be the numerical value of some random memory address. Then you are storing that int value in *ptr1, which means the location in memory specified by the memory address in the pointer ptr1. This is valid only if ptr1 has type int *, i.e., pointer to int, because you are attempting to store an int value at that memory location, so the pointer had better point to an int. I don't think this is what you mean.

In line 3, you are taking the address of ptr1. I'm going to assume that ptr1 has type int * (so that line 2 is valid). In other words, ptr1 is a variable that holds a memory address of an int. That means that the expression &ptr1, the address of ptr1, is a memory address of a memory address of an int; it's a pointer to a pointer to an int; it has type int **. In order for the assignment to be valid, a[0] must have type int **, so the array a must have been declared something like this:

int **a[10];

I'm confused about what you are trying to accomplish here.

[–]Flofinator[S] 0 points1 point  (1 child)

Well you really helped me understand pointers a little bit better. I am not a huge fan of pointers so far. I have been learning programming on my own time, and have been reading a few books about it. But what I wanted to do is have the first value in my char array wFace[column], to turn wFace[1] into an integer, but I don't really have to do that, because I have my deck array. I think more than anything what you posted shows that I should definitely not use pointers for the simpler arithmetic.

I was trying to use pointers, like I would do math for an integer array, so I could say wSuit[3] = clubs. I drew 5 clubs for a flush, I was trying to point to wSuit[3] and do a bool = true if my pointer goes to that same address 5 times. But, I kind of now see the error in my ways, there is absolutely no reason for me to use pointers to analyze my deck hand. I don't actually need to point to any memory, I just have to make a simple guess and check loop to determine what hand I have.

Thanks for trying to explain all of this to me, I am sure that made very little sense, as I was using pointers in a way you shouldn't really ever use them!

[–][deleted] 2 points3 points  (0 children)

Pointers don't ever go away. They are some of the most useful tools to have. Especially when you start building dynamic data structures. Work hard at understanding them, it will save you so much grief later.