Thread: pointers

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    28

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    > 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;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > 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. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by swoopy View Post
    > 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.
    Last edited by Alphawaves; 05-23-2007 at 03:06 PM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    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. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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
    Last edited by itsme86; 05-23-2007 at 03:21 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    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. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That's all a string is -- an array of characters that ends with a '\0' char.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    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. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by Adak View Post
    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. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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
    Last edited by swoopy; 05-23-2007 at 04:47 PM. Reason: Changed len-2 to len-1

  14. #14
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    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. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Make sure you have len-1. I had the example wrong the first time (len-2).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM