Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

Pointers!
Started by Yannbane, Jan 01 2013 04:26 PM
pointers objects variables memory arrays arithmetic c++ c address
30 replies to this topic
#25
Posted 10 January 2013 - 04:39 AM
Compared to any minor (or non - existent) slips in a beginner level tutorial, incomprehensible syntactical jargon is far more likely to drive a beginner away, and cause lack of self confidence in his programming style. Anyway, Evan's straight answer solved my problem, so thanks to him.
I can believe, but why should I?
#26
Posted 10 January 2013 - 01:04 PM
What?! You don't care if you invoke undefined behaviour? In that case, don't come to me when the compiler gods make adjustments your compiler and you notice segfaults galore in your code... As I have said previously, he could simply remove the incorrect content and the rest of the tutorial would still make perfect sense. Since you don't care, I'll leave you be. Good luck with your studies!
#27
Posted 20 January 2013 - 04:27 AM
There's a couple things in this tutorial that I noticed. The reason given for using pointers isn't really a reason at all since function parameters can be passed by value or by reference without pointers. By including the & sign in a parameter list a variable passed will not be copied and the variable at the address will be worked on instead, same functionality you talked about using a pointer for. The main reasons to use pointers are to dynamically allocate memory, create custom memory management allocation, potentially any other memory specific reason you can think of, to point to functions so they can be passed around, and I'm sure there are some reasons from a code efficiency view that could be devised on a case by case basis to speed certain parts of code. The part about not being able to add pointers is wrong since it will work, but it will not work as you may think, instead of adding 1 it will add the size of the memory pointed to times 1.
#28
Posted 21 January 2013 - 06:44 AM
References are implemented as pointers, and there are languages which feature pointers but not references. Therefore, usage of pointers in that way can be useful to know, and it is rather simpler to understand than, say, advanced memory management or something like that.
The point about actually being able to add an integer pointer and an integer is wrong too, at least in C++:
The point about actually being able to add an integer pointer and an integer is wrong too, at least in C++:
a.cpp: In function ‘int main()’:
a.cpp:9:17: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
My blog: yannbane.com. I post about programming, game development, and artificial intelligence.
#29
Posted 21 January 2013 - 08:06 PM
References are implemented as pointers, and there are languages which feature pointers but not references. Therefore, usage of pointers in that way can be useful to know, and it is rather simpler to understand than, say, advanced memory management or something like that.
The point about actually being able to add an integer pointer and an integer is wrong too, at least in C++:
What you're trying to do in your tutorial is add an integer to an integer pointer then assign it to an integer, that gives you a type mismatch error, but adding to a pointer is definitely possible as long as you get your types right. The below program shows how this works. The tutorial made it seem like it was not possible to do math on pointers, but it is.
#include <iostream> using namespace std; int main() { int *mypointer = new int;//int is 4bytes int x = 42; int y = 0; //the memory pointed to by mypointer is the location that x's value is stored mypointer = &x; //have to use * before mypointer to get the data mypointer points to (which is x's value of 42) //without the * before mypointer you will be trying to access the memory location of mypointer //then adding 1 to it and since y is an int (not an int pointer) you will get a type mismatch error. y = *mypointer + 1; cout << y << "\n"; //see that an int is 4 bytes cout << sizeof(*mypointer) << "\n"; //check initial memory address of mypointer cout << (int)mypointer << "\n"; //incrememnt mypointer's memory address mypointer++; //display the new memory address of mypointer cout << (int)mypointer << "\n"; //same syntax as above but left value is an int pointer this time and not an int so no type mismatch mypointer = mypointer + 1; //display the new memory address of mypointer once again cout << (int)mypointer << "\n"; mypointer = NULL; delete mypointer; }
Edited by Zer033x, 22 January 2013 - 07:09 PM.
#30
Posted 21 January 2013 - 09:30 PM
Hey guys, given object's posts in this and other topics, I have decided to remove him from this forum. Sorry if this disrupts this discussion.
Edited by Roger, 21 January 2013 - 09:30 PM.
New around here? Click here to register and start participating in under a minute?
Or do a quick search and you may find the answer you're looking for.
#31
Posted 26 January 2013 - 03:34 AM
@Zer033x: I guess it was a misunderstanding. I didn't really cover pointer arithmetic here, so I meant that the exact example I have shown will not compile.
I see your point though, I could've lead people to believe that it is not possible to do pointer arithmetic, I'll edit that part out.
I see your point though, I could've lead people to believe that it is not possible to do pointer arithmetic, I'll edit that part out.
My blog: yannbane.com. I post about programming, game development, and artificial intelligence.
Also tagged with one or more of these keywords: pointers, objects, variables, memory, arrays, arithmetic, c++, c, address
Language Forums →
C and C++ →
Creating own Loop application in cStarted by radiosmacker, 13 Feb 2019 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
Do you use any debuggers except the standard for Visual Studio?Started by Fernando, 25 Jan 2019 ![]() |
|
![]() |
||
Tutorial Forums →
C/C++ Tutorials →
Basic C++ Game Hacking TutorialStarted by DaDopeman, 12 Aug 2018 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
C++ pointers, references and Dynamic Memory... oh my :(Started by Siten0308, 31 Jul 2018 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
basic c++ question, difference between a and a&Started by Siten0308, 10 Oct 2017 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download