• RSS
  • Twitter
  • FaceBook

Security Forums

Log in

FAQ | Search | Usergroups | Profile | Register | RSS | Posting Guidelines | Recent Posts

Pointers and arrays, working but need opinions

Users browsing this topic:0 Security Fans, 0 Stealth Security Fans
Registered Security Fans: None
Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More

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


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

Offline

PostPosted: Fri Feb 03, 2006 11:22 am    Post subject: Pointers and arrays, working but need opinions Reply with quote

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;
}
Back to top
View user's profile Send private message
Sysop_fb
Just Arrived
Just Arrived


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

Offline

PostPosted: Fri Feb 03, 2006 10:12 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
MattA
Trusted SF Member
Trusted SF Member


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

Offline

PostPosted: Sat Feb 04, 2006 3:43 pm    Post subject: Reply with quote

Cheers I can see that now, I looped through with my count did'nt I.
OK bit more thinking required.
Back to top
View user's profile Send private message
Display posts from previous:   

Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More All times are GMT + 2 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Community Area

Log in | Register