My first question here. I am using an array of pointers instead of a 2D array. Now, to display an element I can use *(arr[i]+j), where arr is the array i denotes the row and j denotes the column. However, when I try to assign a value to any element using the same notation the code stops working. I dont get a compilation error but when I run it it just stops working. Could anybody help me?
Here is my code
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int i,j,k;
char temp2, temp, *arr[] = {
"Brinda Roy",
"Rakesh Bai",
"Neha Saxen",
"Ankit Jain"};
printf("%c",*(arr[3]+8));
for(i=0;i<4;i++){
for(j=0, k=9; j<=4, k>=5; j++, k--){
temp =*(arr[i]+j);
*(arr[i]+j)=*(arr[i]+k);
*(arr[i]+k)=temp;
}
}
printf("\nThe array is ");
for(i=0; i<4; i++){
printf("\n%s",arr[i]);
}
getch();
return 0;
}
arr[i]is equivalent to*(arr+i).for(j=0, k=9; j<=4, k>=5; j++, k--)more specifically these exact charactersj<=4, k>=5. What exactly do you expect to happen here? The comma operator will cause the for loop to only check the right most comparison.int *j = &2; *j = 3;. Where is the number you are trying to change stored? If a pointer points to a constant, you can't de-reference it and modify the result!