GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Jan-2004, 19:59
jack jack is offline
New Member
 
Join Date: Jan 2004
Posts: 15
jack is on a distinguished road

pointers and arrays


suppose you have a code like this in c
a[10]='3' can you give me codes using pointers
(i) i have a function that return the value of 5

for e.g

getd(){
int a;
a=5;
return a;
}

a[10]= must equal the value of 5. how to do it?

(ii)how to allocate the position of 10 by a function if suppose i'm comparing
a[10]=='d';
or if i'm changing the value of 10 to 11
  #2  
Old 14-Jan-2004, 06:47
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Arrays and pointers are closely related. To answer your question, all you need to know really is:

The name of an array is a pointer to the first element of the array.

You know the easy way to get a value from a slot in the array:
CPP / C++ / C Code:
int array[10];
array[0] = 4;
array[3] = 6;
printf("The 4th slot of the array contains &d\n", array[3]);

But you can also obtain values from the array using it's pointer. If you had:
CPP / C++ / C Code:
printf("The address of the first slot of the array is 0x%x\n", array);
you will get the hexadecimal 'address' of the exact piece of memory where '4' is stored. You can then get at this piece of memory by dereferencing the pointer, using the * operator, like so:

CPP / C++ / C Code:
printf"(The value of the first slot of the array is %d\n", *array);

That's all well and good, but what about the rest of the values in the array. Here's the clever bit, you can do arithmetic on pointers. If you add 1 to a pointer, it pushes the pointer on to the next slot in the array. This can be done umpteen times (just don't go beyond the arrays' bound, there's nothing to stop the pointer, and it will point at other completely seperate bits of memory).

So to get the 4th element of the array:
CPP / C++ / C Code:
printf("The value of the third slot of the array is %d\n", *(array+3));
Notice the brackets, if you type *array+3, operator precedence will do (*array)+3 = 4+3 = 7. Not what you wanted.

Addition, subtraction, incrementation & decrememtation all work on pointers, mult & division are not defined.

To answer your questions specifically: (i) have the function recieve a pointer which places 5 at the place the pointer's at, and then call it using getd(array+5) [b]or[/B ]getd(&array[5]).

The & operator gives a pointer to the hexadecimal address of the place the memory is stored.

I don't quite get your second question, but you should be able to figure it out if you understand pointers & arrays after the above.
GF
  #3  
Old 14-Jan-2004, 23:39
jack jack is offline
New Member
 
Join Date: Jan 2004
Posts: 15
jack is on a distinguished road

pointers and arrays problem


Thnx fo the explanation on pointers it was very helpful. But i can't get one thing right. suppose you have an array that accept only one character that is
char a[5];
a[0]='2';
Here I don't get the 2 but i get a value of 52. why is this so? and can can you give a solution for that.

Finally the value of 2 is returned by a function.
for e.g
int gat()
{
//a calculation returning only one character
return a;//where a is the result of the calculation
}

and in main we have
{
char a[5];
char x;
x=(char)gat();
a[0]=x;
}

This does not work because i need the ' ' (single quote ) between x.
Can you give me a solution?
thnx
  #4  
Old 15-Jan-2004, 10:28
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
You'r eplaying around a lot with ints and characters, and converting one into the other, not a well defined operation.

You're placing the character '2' into the array, but trying to use this as an integer will use it's ascii code number: 50. You should have your gat() function returning a character, not an int.

GF
  #5  
Old 15-Jan-2004, 13:27
jack jack is offline
New Member
 
Join Date: Jan 2004
Posts: 15
jack is on a distinguished road

pointers and arrays


It is where my problem lies. The function is supposed to generate and return a random number. And this random number will be allocated to an array upon called.
But my problem is that the array accepts only value between single quotes
for e.g a[1]='2'
The random function is supposed to return the 2 to array a. But i need a value between single quotes not just a 2 like this. hope that you understand the problem and help me.
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.