Thread: Can someone please explain a few things?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    69

    Can someone please explain a few things?

    I have added in comments on the parts that I don't quite understand. Please any links and/or information you find relevant to my questions. Don't point me to a link on pointers, I know what they are, just don't understand some situations they are used in. I would love to have these two questions answered, they really bother me.

    Code:
    #include<stdio.h>
    
    
    	int main()
    	{
    		int *ptr_one;
    
    
    		ptr_one = (int *)malloc(sizeof(int)); //Why has this been type-casted? 
    
    
    		if (ptr_one == 0)
    		{
    			printf("ERROR: Out of memory\n");
    			return 1;
    		}
    
    
    		*ptr_one = 25; //Why do these have to have the asterisk in-front?
    		printf("%d\n", *ptr_one);
    
    
    		free(ptr_one);
            getchar();
            
    		return 0;
    	}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by inu11byte
    Why has this been type-casted?
    Maybe for compatibility with C++. It is unnecessary to cast void* to int* in C since the conversion is implicit.

    Quote Originally Posted by inu11byte
    Why do these have to have the asterisk in-front?
    Since you know what pointers are, the answer to this question is obvious
    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
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by laserlight View Post
    Maybe for compatibility with C++. It is unnecessary to cast void* to int* in C since the conversion is implicit.


    Since you know what pointers are, the answer to this question is obvious
    Lol, nicely played. But seriously, why did they put the asterisk there? I can't find why they programmer did that. I thought you only needed it there to declare a variable as a pointer.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by inu11byte
    But seriously, why did they put the asterisk there? I can't find why they programmer did that. I thought you only needed it there to declare a variable as a pointer.
    Oh. I actually was serious when I said that it was obvious: think, if you have a pointer that points to an object, how do you get to that object through the pointer such that you can say, print the object or change it?
    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

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    * dereferences your pointer, all it means is take what the pointer is pointing to.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Maybe for compatibility with C++. It is unnecessary to cast void* to int* in C since the conversion is implicit.
    <stdlib.h> has not been #include'd in the original post, so the code would also not compile as C without the type conversion (aka cast). The type conversion is therefore being inappropriately used to stop the compiler complaining about an error by the programmer, and the code "as is" gives undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    Quote Originally Posted by inu11byte View Post
    Lol, nicely played. But seriously, why did they put the asterisk there? I can't find why they programmer did that. I thought you only needed it there to declare a variable as a pointer.
    My understanding is after declaring the pointer variable, it works like this:

    *ptr1=25; This means change VALUE AT the memory location pointed to by ptr1 to 25.

    ptr1=25; This means CHANGE MEMORY LOCATION POINTED AT to 25;

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by A34Chris View Post
    My understanding is after declaring the pointer variable, it works like this:

    *ptr1=25; This means change VALUE AT the memory location pointed to by ptr1 to 25.

    ptr1=25; This means CHANGE MEMORY LOCATION POINTED AT to 25;
    Yes that would be correct.
    Note that it makes no sense to set a pointer equal to 25. That isn't anywhere you can read or write from in a modern PC.

    Code:
    *ptr_one = 25;
    
    // is the same as:
    
    ptr_one[0] = 25;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by A34Chris View Post
    My understanding is after declaring the pointer variable, it works like this:

    *ptr1=25; This means change VALUE AT the memory location pointed to by ptr1 to 25.

    ptr1=25; This means CHANGE MEMORY LOCATION POINTED AT to 25;
    I thought the second example had to have an ampersand (&) at the front.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by inu11byte
    I thought the second example had to have an ampersand (&) at the front.
    It is treating 25 as an address, hence iMalc's observation in post #8. Anyway, using the address-of operator & here would not work because you cannot take the address of an integer literal.
    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

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    I found my answer. Binky Pointer Fun Video
    Thanks to everyone that helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 More Things??
    By dolfaniss in forum Windows Programming
    Replies: 7
    Last Post: 02-20-2011, 09:29 PM
  2. 2 things.
    By adr in forum C++ Programming
    Replies: 7
    Last Post: 01-23-2006, 07:10 PM
  3. Just a few things ...
    By AHCB in forum C++ Programming
    Replies: 10
    Last Post: 11-08-2005, 03:10 AM
  4. Few things...
    By LouDu in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 03-30-2003, 12:47 AM
  5. Outside Things....
    By KrAzY CrAb in forum C Programming
    Replies: 3
    Last Post: 06-02-2002, 11:13 PM