Thread: pointers

  1. #1
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28

    pointers

    Hi all,

    Can you confirm im passing my pointer to and from the function correctly? In some examples I have seen them pass the address - I have also passed address?

    Lastly I have read the only math you can perform on a pointer is ++ and --. After thinking about this, I think the mean you can only perform these operations on the address but not the pointer?

    I have included my pointer example below for your review.

    Code:
    #include<stdio.h> 
    
    char add_number(char *ptr)
    {
    	*ptr = *ptr + 10;
    	return *ptr;
    
    }
    
    int main()
    {
    	char result;
    	char name = 2;
    	char *ptr;
    	ptr = &name;
    	
                    result = add_number(ptr);
    	printf("%d\n", result);
    	
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Can you confirm im passing my pointer to and from the function correctly? In some examples I have seen them pass the address - I have also passed address?
    If your aim is simply to play around with pointers, then yes, it is correct. Note that add_number changes the character passed, so you could actually write:
    Code:
    add_number(&name);
    printf("&#37;d\n", name);
    Lastly I have read the only math you can perform on a pointer is ++ and --. After thinking about this, I think the mean you can only perform these operations on the address but not the pointer?
    A pointer is a variable whose value is an address. You can add/subtract an integer to/from a pointer assuming that the result is another pointer to somewhere in the same array, or one past the last element of the array. If two pointers point to the same array or one past the last element of the same array, you can subtract one from the other, and the difference will tell you how far apart in the array are the elements that they point to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A pointer is a variable that contains an address, so naturally you perform these functions on the pointer itself. You can use any number of operations (at least I think) that you can use on integers to manipulate them. The most commonly used operators are probably ++, --, += or -=.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    Thanks for your help and tips. Im working in an embedded enviroment at the minute and you guys and girls seem to be the most helpful on C and its nuts and bolts.

    Im very black and white in my coding, I either use pointers or I dont. Previously I avoided them like the plauge. Now im making an effort to write all my programs using mostly pointers - Is this correct?. For time critical operations, if the above example was writen with or without pointers, is the speed much different? I now we are probably talking pico seconds, but is there a speed increase (faster) with pointers?

    Look forward to your reply.

    RocketMan

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pointers will probably slow things down by a micro-fraction since an additional redirection.
    Pointers are useful, but you shouldn't abuse them. Avoid allocating things on the heap.
    But using pointers to traverse a string or an array is never wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Avoid allocating things on the heap.
    Unless of course you need a lot of memory or you don't know how much memory you need at compile time.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Avoid means don't do it if there's no viable reason to do so
    And the scenario you suggest is a viable reason to do so!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Quote Originally Posted by RocketMan View Post

    Code:
    #include<stdio.h> 
    
    char add_number(char *ptr)
    {
    	*ptr = *ptr + 10;
    	return *ptr;
    
    }
    
    int main()
    {
    	char result;
    	char name = 2;
    	char *ptr;
    	ptr = &name;
    	
                    result = add_number(ptr);
    	printf("%d\n", result);
    	
    }
    Um, what's this doing? What is result, there's no type with it? How do you assign an int, 2, to a chartype var, name? Result looks like it need to be declared with type int. What are you trying to do?

    -CG20

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CodeGeek20 View Post
    Um, what's this doing? What is result, there's no type with it? How do you assign an int, 2, to a chartype var, name? Result looks like it need to be declared with type int. What are you trying to do?

    -CG20
    Code:
    char x = 2;
    is perfectly valid. It sets x to the value 2, as you would expect. It is also perfectly valid to do:
    Code:
    int y = 'a';
    This will set y to the value 'a', which would be 97 if the compiler is using ASCII-compatible character set.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CodeGeek20 View Post
    Um, what's this doing? What is result, there's no type with it? How do you assign an int, 2, to a chartype var, name? Result looks like it need to be declared with type int. What are you trying to do?

    -CG20
    char is not only used for text characters, but it's an integer type with a range [-127, 128] or [0, 255] for unsigned char. If that range it all you need for what you need to do, then why waste extra space with 2, 4, or 8 byte variables?

  11. #11
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28
    Hi all,

    Thanks for all your feedback.

    The example was just random, and was just used to explain my point.

    As for using char, I use these mostly in embedded c because its kind off a small int and uses less program memory. This is quite important in microcontrollers, but in pc programming most people just use ints.

    Thanks again for your tips.

    Cheers

    RocketMan.

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. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  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