1. ## pointers

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int a, b, c;
int* x;
int* y;
int* z;
int* p;
int* q;
int* r;
a = 4;
b = 3;
x = &a;
y = &b;
z = &c;
z = a * b;
printf ("a = &#37;d, b =% d, c = %d", *x, *y, *z);
system("PAUSE");
return 0;
}```
Im trying to get the value of c by muliplying a times b using pointers y and x ( z points to c), but from what i understand only addition and subtraction can be done through pointers. Dont mind the other variable and crap, i just havent cleaned it up yet.

2. > but from what i understand only addition and subtraction can be done through pointers
That's on pointers themselves, not what they point at.
pointer + integer produces another pointer.
pointer - pointer produces an integer.
Given something like p[ i ], which can be written as *(p + i)
We can deduce (from simply rearranging expressions)
q = p + i;
i = q - p;

> c = a * b;
would be
*z = a * *y;

3. > z = a * b;
I think what you want is:
Code:
`      *z = a * b;`
Or you could also write:
Code:
`      *z = *x * *y;`
Since z is a pointer, to get the value pointed at, you need to write *z.

4. Originally Posted by swoopy
> z = a * b;
I think what you want is:
Code:
`      *z = a * b;`
Or you could also write:
Code:
`      *z = *x * *y;`
Since z is a pointer, to get the value pointed at, you need to write *z.
I think i tried the second statement there, but when i compiled the compiler just crashed. Ill take a look.

edit: yes i could do the above statement but then i wouldnt be using pointers x and y to do the multiplication, which is what i need to be doing.

edit: yeah i think the first time i didnt use the pointer *z but rather z in my statement, and everything just went haywire from there.

5. Everything looked good up to here:
Code:
`z = a * b;`
What you want to do is store the value in the memory area pointed to by z. Try changing that line to:
Code:
`*z = *x * *y;  // order of operations gives you this explicit statement: is *z = (*x) * (*y)`
i understand only addition and subtraction can be done through pointers
That's referring to pointer arithmetic. For instance, if you had:
Code:
`char *str = "hello";`
Then you can do *(str + 4) to get the 'o' (addition). Or *p = str + strlen(str) and then do *(p - 1) to get the 'o' (subtraction). But multiplying or dividing str or p is completely meaningless. What you're doing in your code is multiplying the values stored in the memory the pointers are pointing to, not multiplying the value of the pointers themselves which is big no-no.

EDIT: Wow, I was really slow getting this posted I guess.

6. Allright well i have another question now relating to strings, ill post it here because i dont feel like cluttering up the forums with small questions.

If i have a string, char str1[81]; that i have used fgets to input data into is there any way that i can make a char str2[81]; that i can put str1 into but in reverse order? Im trying to make a program that detects pallindromes what i want to do is reverse the string then use a loop that outputs true or false depending on what strcmp says about the two strings.

7. Simple. Here's one way:
Code:
```int index1, index2;

// Find the end of str1
for(index1 = 0; str1[index1] != '\0'; index1++)
;
index1--; // Move back to the last real char of the string

for(index2 = 0; index1 >= 0; index1--, index2++)
str2[index2] = str1[index1];
str2[index2] = '\0'; // terminate str2```

8. I didnt know you could specify a specific character in a string like an array, though i guess i should have... cheers to paying attention in class.

I actually used a loop very similar to this for reversing arrays a few days ago, lol...

9. That's all a string is -- an array of characters that ends with a '\0' char.

10. yeah that makes sense, now im having trouble with the coding....
Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
char str1[81];
char str2[81];
int i1, i2;
printf("Enter a string: ");
fgets(str1, sizeof (str1), stdin);
for(i1 = 0; str1[i1] != '\0'; i1++);
i1--;
for(i2 = 0; i1 >= 0; i1--, i2++)
str2[i2] = str1[i1];
str2[i2] = '\0';
printf("%s %s\n", str1, str2);
if (str1 == str2)  /*originally had if (strcmp (str1, str2) == 0)*/
printf("Pallindrome\n");
else printf("Not Pallindrome\n");
system("PAUSE");
return 0;
}```
The damn thing aint working, it doesnt return the correct result with either if loop.

11. Two things to check:

1) When you print out the original and reversed strings, are the letters all in proper order?

2) When you look at them in a watch window, do both strings still have their end of string marker char: '\0' ?

No marker, no strings (just a bunch of char's), and strcmp() won't work, all of a sudden.

Go back to using strcmp(string1, string2) == 0. No way to do a direct comparison ala string1 == string2.

Two things to check:

1) When you print out the original and reversed strings, are the letters all in proper order?

2) When you look at them in a watch window, do both strings still have their end of string marker char: '\0' ?

No marker, no strings (just a bunch of char's), and strcmp() won't work, all of a sudden.

Go back to using strcmp(string1, string2) == 0. No way to do a direct comparison ala string1 == string2.
Yeah i was just being desperate. My program prints out both the strings before if tests them to see if they are pallindromes. So yes everything is in order, theres just something wrong with my loop coding methinks.

13. fgets() appends a newline('\n') to the string if there's room, so you need to remove it:
Code:
```      int len;

fgets(str1, sizeof (str1), stdin);
len = strlen(str1);
if (str1[len-1] == '\n') str1[len-1] = '\0';```
See the FAQ entry here: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

14. Still not working, ive been looking at it and i think it might be something to do with the fact that the strings are 81 characters long and the method im using to reverse them. so if i inputted lol it would come up as "lol "
and the reverse of that would be " lol" which arent the same using strcmp thus it would print not pallindrome every time.

15. Make sure you have len-1. I had the example wrong the first time (len-2).