Thread: how to pass in a global array

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    how to pass in a global array

    lets say I have an array up here
    Code:
    char array[5][80] = {"banana","apple","grapes",
                                   "lemon", "tomato"};
    its a globally declared array

    and in a function somewhere I need to pass it in through the printf

    Code:
    printf("%2d%s%20.2lf\n", i, array[5], t[i]);
    whats the proper .. I think syntax is the word. . to pass it through printf so it displays all
    5 items in the array alongside the other two variables im passing through printf??

    It is only displaying for ex. Banana when i compile and run it

    thanks..

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    To print more than one element of an array you must either use a loop that iterates through the desired elements or directly print the desired elements
    Code:
    char array[5][80] = {"banana","apple","grapes",
                                   "lemon", "tomato"};
    printf("%s %s %s %s %s\n", array[0], array[1], array[2], array[3], array[4]);
    And remember that arrays start at 0 and end at size -1, 0 - 4 in this case.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Thanks Jim

    I tried the code you listed but it lists then 5 times like this

    Banana Apple Grapes Lemon Tomato

    I am trying to get it to say

    Banana
    Apple
    Grapes
    Lemon
    Tomato

    vertically like that instead of horizontally, any suggestions?

    if I use %s 5 times thats what causes it to be listed horizontally 5 times

    is it possible to pass all 5 values from the array into the function and have it spit out it like this:

    Banana
    Apple
    Grapes
    Lemon
    Tomato

    ??

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sounds to me like you could use a new line after each word. (HINT HINT HINT)


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    I understand what you mean but I am also passing in a 2nd array and a variable in the same function

    I got it to display the first 4 by changing it to array[i]

    Code:
    for (i = 1;i<c;i++)
                   
                       {
                            printf("%2d%10s%15.2lf\n", i, array[i], t[i]);
                            //display type #, array, and the cost
                            total += t[i];
                            }
                            printf("%22.2lf\n", total); //display total
    problem is since i was initialized to 1 up there it doesn't display the 0th position in the array, I need i at 1 although for the 2nd array t[i] that i am passing through

    now im in a dilemma :s

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Usually, arrays with data will need all their data to be printed.

    If you have a second array, and you only want to print up 1-4 words that it's holding, instead of 0 - 4, then use an if statement:

    Code:
    for(i = 0; i < NumberOfWords; i++) {
      print(string of array1)
      if(i > 0)
        print string of array2
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Pass array through functions
    By willc0de4food in forum C Programming
    Replies: 13
    Last Post: 03-06-2005, 10:53 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM