[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Print an array

Posted on 2006-06-06
4
Medium Priority
?
199 Views
Last Modified: 2010-04-15
Hi,

I have an arry (char a[10]). I want to print all elements in the array with printf. I do not want to write sth like printf ("%c ... %c",a[0]...a[9])

Is there a convenient way to do that?

Thanks
0
Comment
Question by:tiger0516
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 200 total points
ID: 16847957
Hi tiger0516,

You are looking for a loop. It sounds to me like a 'for' loop would be best. They are used to do something with a sequence of items. Post your best guess at a loop you think should work and we'll check it for you. Dont worry about the printing right now, just think about where the loop start (hint: at 0), what will make it end (hint: 10 or more) and what you need to do each time the code in the loop completes (hint: step forward by 1).

Post us some code! I like a good homework question but you will have to do the work. :-)

Paul
0
 
LVL 1

Author Comment

by:tiger0516
ID: 16847976
Hi,

Thanks. I know a loop will work but I want to keep the printf comman consise. I will define a poiter to a self defined struct, and then I print the pointer, I think that should work. Right?
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16848040
Hi tiger0516,

Essentially yes but focus on the loop first. You know you can access 'a[0]' 'a[1]', you can also access 'a[i]'. Once you have that, the printf command is trivial so just focus on the loop right now. Post your loop and I'll check it.

Paul
0
 
LVL 1

Author Comment

by:tiger0516
ID: 16848427
Hi Paul,

I am using struct, and it works.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question