C++/pointers

Advertisement


Question
What is the difference b/w
a)char a[]="my name is"
b)char *p="my name is"
in terms of memory allocation and accessing individual elements.

Answer
Dear Praveen,
Thankyou for your question.

Well, as far as your first notation is concerned

a) char a[] = "my name is";
its memory location is like this

100 101 102 103 104 105 106 107 108 109
'm'  'y' '' 'n' 'a' 'm' 'e'

It would be stored something like this in the memory location.
Which means that, arrays occupy the continguous memory location. The null character is appended by default.

b) In your second notation, the base address of your
first character is stored in the pointer, then you've
to increment the pointer to access the second alphabet
which confines the fact that, arrays are stored in
contingous memory locations.

There is no particular difference, these are two ways
to express the same fact, just pointer notation is a
bist fast.

Hope this helps,
Regards
Professional

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Professional

Expertise

I can answer any question about functions,pointers,structures,object oriented programming basics of classes and data structures.My strong field is structured programming.

Experience

I have got 2 years experiece under C . I am able to answer about the structured concepts pointers to a little extent,OOP concepts. I have also experience in data Strucutres like Linked List, Stacks , Queues, Heaps, B Trees, Red Black Trees. I will try to satisfy with my knowledge. I am the Student of an expert here Martin, what i have learnt today, i just owe my every knowledge to him. He is the greatest.

©2016 About.com. All rights reserved.