Will there be any change in the contents of an integer array if a char pointer points to it?
Both can't point to same location and until you use integer pointer .
well if you got an array of type int: int array [ ] you need to use a int pointer, also the array must be dynamic if you planning to change their contenst, unless you create a new one with 0s on it of a certain quantity.
int array [ ] = { 0, 0 , 0 , 0 }; // this array contains 4 numbers that are zero. for(i=0, i < size(array) i++ ) { array[ i ] = entermenumber(); } // to fill out the array contents. int entermenumber() { int number = 0; cout >> "Enter a numbre that is an integer!!!"" cin << number; return number; } int * p = array [ 2 ]; // this assigns the address in memory allocation of number that is in the second position of the array. Now that pointer of type int points to the address in memory allocation of the array in the pissition where 2 as index is. hope this helps :)
Consider this.... in C int arr[]={1,2,3}; char *p; p=(char*) arr; Now will there be any change in the contents of arr?
well thi is silly, you are trying to point an array that is type int with a char pointer.... it has to be int, the pointer so then it can hold the first content in the array there, when you just past arr without puting the index, it is pointing to the first in the array. int *p; p = arr; // Now P points to the first content in arr, that is 1. but I think you are trying to cast the array from int to char? well not sure, try using a compiler and see what's the results from that :)
anyways thks:)
Hey priyan, if we want to point an array using a pointer, we have to use the same kind of data type or we need to use null pointer and then type cast it. And pointer are just like variables which point to a memory location, it does not affect the value stored in that location until and unless you modify that value using pointer in combination with value of (*) operator.
Join our real-time social learning platform and learn together with your friends!