• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Print an array

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
tiger0516
Asked:
tiger0516
  • 2
  • 2
1 Solution
 
PaulCaswellCommented:
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
 
tiger0516Author Commented:
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
 
PaulCaswellCommented:
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
 
tiger0516Author Commented:
Hi Paul,

I am using struct, and it works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now