Looking for some clarification on a specific topic related to addressing an array of strings. There seem to be several related issues but I wasn't able to find one that discussed my question. If this has been asked before please point me to the relevant thread.
In the snippet of code below (check printf statement), I use the same variable to access the value at a memory location and the address of the memory location. I'm not quite sure if this is how I'm supposed to write this piece of code. Is there a better way that will clearly indicate if I'm accessing the address or the value?
char *board[NUM_MAX_ROWS] = {"0101001",
"1101011"};
int main()
{
int i, num_rows=0, num_cols=0;
num_cols = strlen(board[0]);
num_rows = ARR_SIZE(board);
for (i=0; i<num_rows; i++)
printf("%s stored at %p\n", board[i], board[i]);
}
My first attempt looked like this
while(*board != '\0')
{
printf("%s stored ar %p\n", *board, board);
board++;
}
Obviously this doesn't work :) but I'm still not quite sure about how this is interpreted by the compiler.
Thanks.
char*) twice - interpretation of argument then depends on format specifiers.%swill print chars starting at adress and stopping at NUL character.%pwill simply print the adress.board++, array is implicitly converted to a pointer to its first element. That yields an r-value which you're not allowed to increment.