473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

What really pointer is? Should i first learn assembly language before C?

Hello everyone! I'm new in programming.At present i'm learning about Pointers in C but sadly i'm not able to understand it.I'm very much confused.What really pointer is what pointer variable is? what is the difference between them.Especially i became more confused when i read two different books about the same topic.Like for example when i was trying to learn about pointers by K&R.it has written that pointers are variables that contain address of another variable.On the other hand in K.N King it has written that pointers are nothing more than address.What does it mean ? Does it mean that pointer variable store pointer and pointer are the variable address which mean that pointer is nothing but address of any variable ? Here i have uploaded two screenshots of K&R book.In the second screenshot what does "&a is a pointer to a " mean ? That address of a is a pointer but how? I had searched about this topic but couldn't get the exact and clear answer.Some says pointer is a variable Some says pointer are variable.I found this site helpfull many time in comparision to others therefore i posted my doubts in this site rather than other.Please clear my doubts related to what it is written in 2nd screenshot of K&R book . :)
And my last question is that should i learn assembly language first which will help me to learn about memory management and pointers? Please help!
Oct 28 '14 #1
9 1358
weaknessforcats
9,208 Expert Mod 8TB
First, the pointer.

Assume you are going to a party at 123 Fox St. It's pretty normal to write the address on a piece of paper.

That piece of paper is the pointer. The pointer contains the address 123 Fox St. If you go there, you will be at the party.

In C this looks like:

Expand|Select|Wrap|Line Numbers
  1. int party;   /* the party */
  2. int* ptr;  /* the pointer */
  3.  
  4. ptr = &party;  /*write the address of the party on the pointer */
  5.  
To go to the address in the pointer, you de-reference the pointer:

Expand|Select|Wrap|Line Numbers
  1. *ptr = 10;  /* go to the address in ptr and put a 10 there */
  2.  
That's all there is to it. You just need to know at all times whether you are dealing with the thing itself or the address of the thing.

Do not waste time learning assembly code. Just learn the C, or if possible, skip the C and go straight to C++.
Oct 28 '14 #2
Frinavale
9,735 Expert Mod 8TB
Hi,

My name is Frinny and your name is Yogesh0551.

We are both people on earth located in completely different places.

If someone wanted to get a hold of you they would use your name "Yogesh0551" and you would respond.

If someone wanted to get a hold of me they would use my name "Frinny" and I would respond.

Pointers are a lot like our names but instead of referencing people on earth they reference locations in memory that contain data.

When you first declare a pointer it points to memory location 0 which indicates that it is not set. In this case your pointer is null and if you try to access data using a variable pointing to nullyou will get a null reference exception.

To get around null reference exceptions you have to set your pointer to a memory location first.


So, let's examine the following code:
Expand|Select|Wrap|Line Numbers
  1. int  numValue = 20;   /* actual variable declaration */
  2. int  *numPointer;        /* pointer variable declaration */
  3. numPointer = &numValue ;  /* store address of var in pointer variable*/
On Line 1, the numValue variable contains a number in memory.

On Line 2, we declared a pointer and at this point in code it is null.

On Line 3, we are setting the numPointer to the memory location that contains 20 that is being used by the numValue variable. We are getting the memory location placing the & in front of the numValue.

Now both numValue and numPointer are referencing the same memory location.

So if we were to do this:
Expand|Select|Wrap|Line Numbers
  1. numValue = 80;
Both numValue and numPointer now contain 80.

The important thing to remember is that numPointer contains a memory location NOT a value.

In order to get to the value that is stored at the memory location that numPointer is pointing to you need to put a * in front of it.

Like this:
Expand|Select|Wrap|Line Numbers
  1. printf("Address stored in numPointer variable: %x\n", numPointer);
  2. printf("Value stored in the memory location that the *numPointer variable is point to: %d\n", *numPointer);
Make sense?

-Frinny (<--- a pointer to a person who is helping you)
Oct 28 '14 #3
donbock
2,426 Expert 2GB
I find it confusing to compare pointers to names. Variables have names too; and referencing a variable by its name has nothing to do with pointers.
Maybe you will find something in the C FAQs related to Pointers helpful.


Consider a system that reports the intensity of ambient light. It contains a sensor that converts light intensity to voltage; and then that voltage goes to an analog-to-digital converter; and you have to write the program that converts the voltage at the analog-to-digital converter back to light intensity to show to the user. Light intensity sensors are often nonlinear, so you create a structure variable that contains a piecewise linearization of the sensor's transfer function and write a program that uses the structure to convert voltage to light intensity.

You can do all of that using "regular" variables -- no pointers.

Now consider if the system allows the user to select one of several light sensors. Perhaps each sensor is sensitive to a different portion of the spectrum. Each sensor has its own unique structure variable to convert volts back to intensity. If your program knows which sensor is selected, it can set a structure-pointer variable to point at the appropriate structure ... if the rest of the program accesses the selected structure variable by dereferencing the pointer variable rather than by referencing that particular structure variable by name.

This is one case where using pointers allows you to get the job done easier. There are other cases where pointers are useful.
Oct 28 '14 #4
donbock
2,426 Expert 2GB
A couple of basic concepts in software engineering are call by value and call by reference. Pointers are the means by which a C programmer achieves call by reference. That is another circumstance where pointers are useful.
Oct 29 '14 #5
thankyou for your answer but can u please explain me how &a of a is a pointer to a in a functiin for example swap(&a,b).It will be really great of u.Itwill really help me. :)
Oct 29 '14 #6
weaknessforcats
9,208 Expert Mod 8TB
You have to look at the swap function prototype. Maybe it's something like:

Expand|Select|Wrap|Line Numbers
  1. void swap(int* x, int* y);
That tells you that the arguments must be addresses and so the argument variables (x and y) are pointers.
Oct 29 '14 #7
varaible x and y are pointers very true.But what is meant by this &a is a pointer to a? How address of any variable can be the pointer?
Oct 29 '14 #8
Banfa
9,065 Expert Mod 8TB
A pointer stores an address (or a location in memory), that is more or less how it is defined and I hope clear from other posts so far.

Variables have memory allocated to them, so it is possible to get the address of the variable, the address (or location) that it is stored at. Having got the location you can then store it in a pointer.

& is the reference operator (as well as being bitwise AND how the compiler interprets it is defined by context), this operator returns the address of the object to its right as follows
Expand|Select|Wrap|Line Numbers
  1. int a; // a is and integer, it has 4 bytes and is stored in memory
  2.  
  3. a = 6; // Assign the value 6 to a
  4.  
  5. &a;    // The reference operator & returns the address, 
  6.        //that is memory location that a is stored at
  7.        // We don't do anything with it
  8.  
  9. int* p;// p is declared as a pointer to an integer
  10.        // it can hold the memory location or address of an integer
  11.  
  12. p = &a;// The reference operator & is used to get the address (or
  13.        // memory location) of a which is then stored in p
  14.  
The dereference operator * (yes also multiplication again context comes into play) is used to dereference a pointer (or memory address) back into an object (the object pointed to)

Expand|Select|Wrap|Line Numbers
  1. // Continuing from above
  2. int b;  // Create a new integer b
  3.  
  4. b = *p; // Dereference p back into an integer and assign to b
  5.         // Because p pointed at the memory location of a
  6.         // it is the value of a that is assigned to b
  7.         // Therefore b has the value 6
  8.  
Oct 29 '14 #9
weaknessforcats
9,208 Expert Mod 8TB
@Yogesh0551

You are correct that the address of a variable is not a pointer. It's just an address. However, when you store that address in a variable, the variable is the pointer.
Oct 29 '14 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Aziz McTang | last post by:
Hi Group, I am not an experienced programmer at all. I've learned html and css well enough to hand-write simple websites. I'm now looking to move to the next step. Initially, I'd like to do 3...
72
by: Mel | last post by:
Are we going backwards ? (please excuse my spelling...) In my opinion an absolute YES ! Take a look at what we are doing ! we create TAGS, things like <H1> etc. and although there are tools...
26
by: nospam | last post by:
Just wondering, What do you think the difference in performance would be between (1.) Compiled C# (2.) Compiled C++ (3.) and Assembly Language And how would the mix be if some if any of...
8
by: Chin Fui | last post by:
I am writting a CD-ROM Emulator application using assembly language and link with the VB.NET interface. But, I am not sure whether assembly language is linkable with VB.NET. Another question is...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
13
by: Robert Cloud | last post by:
Is it possible to include assembly language routines in C if I'm using a compiler which has an assembler such as gcc? could I include them in a main function or would I have to write a seperate...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.