View previous topic :: View next topic |
Author |
Message |
MattA Trusted SF Member


Joined: 13 Jun 2003 Posts: 16777193 Location: Eastbourne + London

|
Posted: 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
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;
}
|
|
|
Back to top |
|
 |
Sysop_fb Just Arrived

Joined: 27 Sep 2005 Posts: 0 Location: Iraq for the moment

|
Posted: 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.
|
|
Back to top |
|
 |
MattA Trusted SF Member


Joined: 13 Jun 2003 Posts: 16777193 Location: Eastbourne + London

|
Posted: 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.
|
|
Back to top |
|
 |
|