Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Arrays,Strings,LinkedLists and pointers


  • Please log in to reply

#1
BuRnEr

BuRnEr

    Member

  • Member
  • PipPip
  • 48 posts
Arrays, Strings and Linked Lists page 1

 Arrays
Arrays are data structures which can hold same size or same type data. Arrays also known vector or list.
Arrays can be one or more (two) dimensional.
Example: Simply Array can be declared
dataType ArrayName (dimension);
• Arrays are fixed length
• Elements are beside of eachother
• Access members of array using an integer index whose value lies between zero and the number of elements.
• Base type of an array’s elements can be any valid (primitive or user defined) type or class.
• Array can be allocate compile time(static array) and run time(dynamic array)
Example:
 Static Array
Int Array [50]; double Array [50]
 Dynamic Array can be done using pointer
Int *Array; double *Array;
Array=new int [10]; Array =new double [10];
If array allocated dynamically, then it must also be deallocated (or deleted)
delete [] Array;
• Two dimensional arrays deceleration
Example:
Int Array [] []; double Array [] [];
 Note: when you initializing array length or index, it must be initialized by const.
Example:
const int index = 50;
Int Array [index];
--more about arrays can be found at:
http://www.functionx...pp/Lesson11.htm

 Strings or “character of arrays”
Strings are simply an array whose data type is char. The contents of the string is the sequence of characters stored in its elements, up to but excluding the first occurrence of the null character (corresponding to integer value Null terminating character=\0).So, the length of the string must be at least one more than the length of the character sequence that it stores.
 Note: The C++ and its parent the C languages do not have a string data type. In C and C++, strings are created from arrays. Therefore, the C++ language relies on operations preformed on the arrays of characters or pointers to char.
• Ways to declare and initialize a strings
Example:
char myFirstName [6] =”burner”;
myFirstName[0]=’b’ myFirsName[1]=’u’ myFirstName[2]=’r’ myFirstName[3]=’n’ myFistName[4]=’e’ myFirstName[5]=’r’ myFirstName=’\0’
A string can be allocated with more space than it needs.
myFirsName [20] =”burner”; page 2
Char MyFullName [] =”burner heaven”;
Char *Name=”burner”;
• Strings can be allocated dynamically as well as statically, since they are array in C++
Example:
char *myFirstName;
myFirstName= new char [7];
It must be deallocated as same as arrays (or deleted)
delete [] myFirstName;
 Strings can be also created Multidimensional arrays of characters
Example:
char Animals [4] [10] = {“Cat”,”Dog”,”Lion”,”Cow”};
--more about string can be found at :
http://www.cplusplus...ial/tut3-2.html


 Linked List
An array allocates memory for all its elements lumped together as one block of memory. A linked list allocates space for each element separately in its own block of memory called a “linked list element” or “node”. The list gets is overall structure by using pointers to connect all its nodes together like the links in a chain.

• Each node contains two fields: a “data” field to store whatever element type the list holds, and a “next” field which a pointer is used to link one node to the next node.
• Each node is allocated in the heap is that means linked list are dynamical data structures.
• Linked lists useful when we don’t know how many elements there will be, and if elements need to be inserted and deleted frequently, it is better to use a linked list. This is because each element for linked list is created as needed and can be stored anywhere in the free memory space the elements in the list do not have to be stored in contiguous locations
• Creating node and linking it with another node;
Example:
Node *next;
int Data; or char Data [20];

temp =new node;
temp1=new node;
temp->next=temp1
temp1->next=Null


data-->data-->Null
|temp| |temp1|

the "-->"=="next" is a pointer.
---more about linked lists can be found at:
http://richardbowles...st/linklist.htm


 Pointers
Pointer is the variable that stores the address of another variable.
• Declaring and initializing pointers
type *pointer_name; page 3
Example:
int value=5; or int value=5;
int *pointer=&value; int *pointer;
pointer=&value;
 Note: Each one points to a different data type, but the three are pointers and in fact the three occupy the same amount of space in memory (the size of a pointer depends on the operating system)

• Arithmetic of pointers
Only addition and subtraction operations are allowed in C++ pointer arithmetic’s. But both addition and subtraction have different behavior with pointers according to the size of the data type to which they point.
Different data types occupy more or less space than others in memory.
Example:
Char =>1 byte int =>4 bytes
Short =>2 bytes float =>4 bytes
Long =>4 bytes double =>8 bytes
If we have three pointers how should be done add and subtract
Example:
char *pchar; address is 2000
int *pint; address is 3000
double *pdouble; address is 4000

pchar++; or pchar=pchar+1; now address is 2001
pint++; or pint=pint+1; address is 3004
pdouble++;or pdouble=pdouble+1; address is 4008
pdouble=pdouble+4; address is 4032

Same thing can be done by (-) subtraction it’s just decreasing the address of pointer opposite way (+) adds does.
 Note: It is important to know that both increase(+) and (-) operators have a greater priority than the reference operator asterisk (*), here this expression may lead to confusion:
*p++ ->this means increase content of p by one.
*(p++) ->this is that what we did exactly above

• Pointers to pointers
C++ allows us to point to pointer with another pointer.
Example:
Char a; and a =’a’;
Char *b; b =&a;
Char **c; c =&b;
c is a pointer to point b ‘ s address and b is a also pointer to variable a ` s address.

page 4
• Pointers and Arrays
The identifier of an array is equivalent to the address of its first element, like a pointer is equivalent to the address of the first element that it points to, so in fact they are same thing.
Example:
Int numbers [20];
Int *p; so we can do this p = numbers;
char *name=”temel”;
char *p=name;
name and p are equal address of ‘t’ character.
 Note: But we can not do this
numbers = p;
because of that numbers(array name) is a constant pointer and we can not change the constant value.

• Dynamic Allocation
It is used to allocate memory without having to define variables and then make pointers point to them.
Example:
Int *pNumber;
pNumber = new int;
And should be deleted after used it.(freeing the memory)
delete pointer_name;
In this case
delete *pNumber;

pointers magic of C++ yyeesss they are so smart and they can do everything ,...
you can find more about it :i would say that if you know pointers very well that means you know C++ and memory no kiding...
spend most of your study time with pointers even you forget about your girlfiriends or boyfriends for a wihile and date pointers for a while...kidding ;)

http://www.intap.net...pp/cpp08_01.htm
http://www.robertjac...o.uk/point1.htm
http://www.kcl.ac.uk...an/cpp/CH08.htm

I think this tuto is going to be little usefull who really thinks about programming
and this topics are the most difficult topics in programming...
i used C++ in this tuto because C++ is everything even os unix,windows,
it is being used in medical,all kind of math calc, engineering,arts , games and so on ...

i also say that "before you start to code something first think and designe what you are going to do and put it on pice of paper, then you code it into the desired language .. if you dont create logical thing there is no meaningfull coding ...

without thinking you can only create that cout<<"Hello World"<<endl; or printf "this is my first program"; or print "say Hello World again and over again"; or Console.WriteLine("Now Say Byeee");
or you prefer this:
#include <iostream>
using namespace std;
swapUs(int u,int v){int aux; aux=u;u=v;d=aux;
cout<<"\n\n\tin swap() a="<<u<<",b="<<v;
int main()
{ int a,b;
cout<<"a="<<a<<"b="<<b<<"\n"<<endl;
swapUs(10,20);
cout<<"after Swap in main\n";
cout<<"a="<<a<<"b="<<b<<"\n"<<endl;}

copy and paste the source code into your compiler and run it ...programming is fun and i m sure that you can do more fun(things) more than i do.you will find yourself in the deep night awake...how about in your dreams ( :tazz: )
we all do sometimes make mistake...
so just take kare
  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP