![]() |
|
#1
|
|||
|
|||
pointers and arrayssuppose 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
|
|||
|
|||
|
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:
But you can also obtain values from the array using it's pointer. If you had: CPP / C++ / C Code:
CPP / C++ / C Code:
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:
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
|
|||
|
|||
pointers and arrays problemThnx 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
|
|||
|
|||
|
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
|
|||
|
|||
pointers and arraysIt 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 GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The