We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

function / array help

does any one know why my for loop work fine but printf("%c",num[0]); get me 0?

void test(char[]);
void test2(char[]);

int main(void)
{
  char num[10];
  test(num);
  test2(num);
}

void test()
{
   num[0] = '1';
   num[1] = '2'
}


void test2(int num[])
{
  for(a = 0; a < 10; a++)
 {
  printf("%c",num[a]);


  printf("%c",num[0]);

  
 }
}
3
Contributors
2
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
hwoarang69
Posting Pro
569 posts since Feb 2012
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 7

That doesn't compile.

histrungalot
Posting Whiz in Training
280 posts since May 2008
Reputation Points: 76
Solved Threads: 36
Skill Endorsements: 1

The num[] array is a local variable to main(). You need to make it global for this to work. Just move the declaration outside of main, as in:

void test(char[]);
void test2(char[]);

char num[10];
 
int main(void)
{
  test(num);
  test2(num);
}
 
void test()
{
   num[0] = '1';
   num[1] = '2'
}
 
 
void test2(int num[])
{
  for(a = 0; a < 10; a++)
 {
  printf("%c",num[a]);
 
 
  printf("%c",num[0]);
 
 
 }
}

It's still beginner's code, but it should work (not tested, not certified).

rubberman
Posting Maven
2,569 posts since Mar 2010
Reputation Points: 365
Solved Threads: 305
Skill Endorsements: 51

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0605 seconds using 2.68MB