Overclock.net banner

[c++]pointers

443 Views 8 Replies 6 Participants Last post by  DesertFox
im trying to understand pointers but its not very clear to me.

Code:

Code:
char *a;
a = "string";
why does this work?
until now i was thinking that pointers allocate memory to store the address of a variable and some memory for the variable itself but if that was correct the above code wouldn't be possible because char only stores 1 character.could somebody explain this to me?
1 - 9 of 9 Posts
Yes, but a pointer "points"to, it does not contain, remember?

Does that help?
  • Rep+
Reactions: 1
yes it points to the place where char a is but why does it make char a into a char array ? since a string is larger than a char then it also has more than one address of memory meaning that it can't be stored in a pointer.
a string is really an array of char.
and the pointer is indicating the first character of the string(the char array).
in cases like this (ex. array of int ) the pointer points to the first member of the array.
for example in your case a -> "s".

if you want to know the pointers better test these :

char * a;
a= "string";
cout<< a << endl ;
cout<< *(a) << endl;
  • Rep+
Reactions: 1
Well, first, this is a C string, not a C++ string. They were implemented as arrays of char for various efficiency reasons which certainly made sense in the 1970s (and probably still make sense today; ever notice how code gets bloated over time?) ...
2
thanks for the help! i somehow thought that pointers were showing the address of a variable and not the value and because of that i started confusing stuff in tutorials.

Quote:


Originally Posted by error10
View Post

Well, first, this is a C string, not a C++ string. They were implemented as arrays of char for various efficiency reasons which certainly made sense in the 1970s (and probably still make sense today; ever notice how code gets bloated over time?) ...

didn't notice that but thats just probably because im still a newbie at programing


oh and a quick question : what are the advantages of object oriented programing? to be more specific : what are the advantages of classes? to me it just looks like simple functions with some kind of encryption to keep safe of the variables.
See less See more
Classes and object oriented programming are just syntactic and mental constructs. Primarily they are to help people make better, more complex programs with fewer errors. The computer doesn't care one way or the other. It'll try to execute anything, whether it makes sense or not (e.g. Windows).
2
Quote:

Originally Posted by newbie1911 View Post
...what are the advantages of classes?...
From Object Mentor's website:
Object-oriented design is a programming paradigm that began in the late 60's as software programs became more and more complex. The idea behind the approach was to build software systems by modeling them based on the real-world objects that they were trying to represent. For example, banking systems would likely contain customer objects, account objects, etc. Today, object-oriented design has been widely adopted by businesses around the world. When done properly, the approach leads to simpler, concrete, robust, flexible and modular software. When done badly, the results can be disastrous.
There's a lot of good papers to read on the page linked above.
See less See more
  • Rep+
Reactions: 1
That's a good way to fry memory in C, isn't it?
1 - 9 of 9 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top