array questions

This is a discussion on array questions within the C Programming forums, part of the General Programming Boards category; I have the following simplified code. Code: void TestChannels(int *Chan) { int x; for (x=0; x<9; x++) { Chan[x] = ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    array questions

    I have the following simplified code.

    Code:
    void TestChannels(int *Chan) {
      int x;
    
      for (x=0; x<9; x++) {
        Chan[x] = x * 2;         // Problem is here
      }
    }
    
    int main() {
      int  Channel[9];
      int x;
    
      //Initialization
      for (x=0; x<9; x++) {
        Channel[x] = 0;
      }
    
      TestChannels(Channel);
    
      for (x=0; x<9; x++) {
        printf("Channel %d = %d", x, Channel[x]);
      }
    }
    My question is, within the function, how do I refer to the individual elements of Channel?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what is wrong with the code as you have it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,629
    Your use of Chan[x] is correct. However, I suggest adding a parameter for the size of the array to the TestChannels function.

    Note that if you want to do zero initialisation of the array, this would suffice:
    Code:
    int Channel[9] = {0};
    You can then dispense with the loop that sets each element of Channel to 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you expected this:

    Channel 0 = 0Channel 1 = 2Channel 2 = 4Channel 3 = 6Channel 4 = 8Channel 5 = 10Channel 6 = 12Channel 7 = 14Channel 8 = 16

    Then I don't understand how you are having a problem; there is nothing wrong with your code. One tip, tho: you can initialize all the elements to zero this way:
    Code:
    int  Channel[9]={0};
    Which it's not particularly necessary in your code anyway, since the entire array gets written into by TestChannels().

    [edit] you know what they say about great minds.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Hmmm... Well nothing is wrong... I simplified the actual code I had that wasn't working... I will analyze and get back...

    and thanks for the intialization tip
    Last edited by Bladactania; 02-25-2009 at 09:46 AM.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    What about this... this is more how my original was structured...

    Code:
    void VerifyChannels(int *Chan) {
      int x;
    
      for (x=0; x<9; x++) {
        Chan[x] = x * 2;         // Problem is here
      }
    }
      
    void TestChannels(int *Chan) {
      VerifyChannels(&Chan);
    }
    
    int main() {
      int  Channel[9];
      int x;
    
      //Initialization
      for (x=0; x<9; x++) {
        Channel[x] = 0;
      }
    
      TestChannels(Channel);
    
      for (x=0; x<9; x++) {
        printf("Channel %d = %d", x, Channel[x]);
      }
    }
    My error was the & in VerifyChannels(&Chan); It should not have been there.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void TestChannels(int *Chan) {
      VerifyChannels(&Chan);
    }
    This should AT LEAST give you a warning for incompatible pointer types.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21