Pointers and arrays, working but need opinions

Networking/Security Forums -> Programming and More

Author: MattALocation: Eastbourne + London PostPosted: Fri Feb 03, 2006 11:22 am    Post subject: Pointers and arrays, working but need opinions
    ----
Hey All,
Still persevering with the C, whilst this code works, can I have a
quick review from you learned coders Smile
The exercise was to write a program that declares a 12*12 array of characters. Place Xs in
every other element. Use a pointer to the array to print the values to the screen in a grid
format.
My answer was to declare the char array once then loop through it rather than using a
multidimensional array, which I could have done but thought it inefficient and did
not add much to the exercise.
Perhaps I ought to also re code it so it only prints the X's with a spot of pinter
arithmatic....might be a good exercise.
ThanQ all
-M

Code:

#include <stdio.h>
//Declare my array of Chars
char *array[12] = {"O", "X", "O", "X", "O", "X", "O", "X", "O", "X", "O", "X"};

main()
{
//declare array of 12 pointers to type char + other variables
char *ptr[12], count, num2;
num2 = 0;
//Do loop while num2 <12
do
{
// loop thorugh the array printing each member in turn
for (count = 0; count <12 ; count++)
printf("%s",array[count]);
// print and increment num2
printf("\n",num2++);
}while (num2 < 12);
return 0;
}

Author: Sysop_fbLocation: Iraq for the moment PostPosted: Fri Feb 03, 2006 10:12 pm    Post subject:
    ----
I'm not seeing where you're using your ptr at all.

And
Code:
//Declare my array of Chars
char *array[12] = {"O", "X", "O", "X", "O", "X", "O", "X", "O", "X", "O", "X"};


That's not an array of chars that's an array of pointers to chars.

Code:
//declare array of 12 pointers to type char + other variables
char *ptr[12]


If you eventually plan on using that aray of pointers to point to your array of pointers... then you'll need to make that an array of pointers to pointers, not an array of pointers to chars. Unless you change you're char *array to an array of characters instead of an array of pointers.

Author: MattALocation: Eastbourne + London PostPosted: Sat Feb 04, 2006 3:43 pm    Post subject:
    ----
Cheers I can see that now, I looped through with my count did'nt I.
OK bit more thinking required.



Networking/Security Forums -> Programming and More


output generated using printer-friendly topic mod, All times are GMT + 2 Hours

Page 1 of 1

Powered by phpBB 2.0.x © 2001 phpBB Group