Thread: Function interferes with Function

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Function interferes with Function

    I was hoping someone may comment on a behavior I haven't seen before. I've done a work around but this seems so unusual to me I would really like to know why it is happening if possible.
    In the code below I call another function twice within the same function. I've changed the code a bit to make it easier to see while still retaining the behavior from the original. As the code is here you can not get the value printed on the line :

    printw("%c ", nd_base_num[0]);

    although the values at [1], [2], etc will print out.

    Code:
    void multiply_numbers(char *base_num, char *series_start_num)
    
    {
    
    char nd_base_num[MAXLENGTH - 1], nd_series_start_num[MAXLENGTH - 1];
    int base_asint, series_asint;
    
    move(15,0);
    
    remove_decimal(base_num, nd_base_num);
    
    remove_decimal(series_start_num, nd_series_start_num);
    
    printw("%c ", nd_base_num[0]);
    
    printw("%c ", nd_base_num[1]);
    
    printw("%c ", nd_base_num[2]);
    
    printw("%c ", nd_base_num[3]);
    The funny thing is that if I move the print command to sit between the the first function and the second call to that function then the value of [0] will print. This code works;
    Code:
    void multiply_numbers(char *base_num, char *series_start_num)
    
    {
    
    char nd_base_num[MAXLENGTH - 1], nd_series_start_num[MAXLENGTH - 1];
    int base_asint, series_asint;
    
    move(15,0);
    
    remove_decimal(base_num, nd_base_num);
    
    printw("%c ", nd_base_num[0]);
    
    printw("%c ", nd_base_num[1]);
    
    printw("%c ", nd_base_num[2]);
    
    printw("%c ", nd_base_num[3]);
    
    remove_decimal(series_start_num, nd_series_start_num);
    
    }
    Even more strange is that if I exchange the arguments of the first call with the second and try printing the values of the new arguments to the first call then I can get it print the value at the address [0]. This code works:
    Code:
    void multiply_numbers(char *base_num, char *series_start_num)
    
    {
    
    char nd_base_num[MAXLENGTH - 1], nd_series_start_num[MAXLENGTH - 1];
    int base_asint, series_asint;
    
    move(15,0);
    
    remove_decimal(series_start_num, nd_series_start_num);
    
    remove_decimal(base_num, nd_base_num);
    
    printw("%c ", series_start_num[0]);
    
    printw("%c ", series_start_num[1]);
    
    printw("%c ", series_start_num[2]);
    
    printw("%c ", series_start_num[3]);
    
    }
    The arguments the initial function takes ranges between 0 and 9, I've tried the different values and the code still behaves the way it does. Any feedback is appreciated

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What that tells me is that your remove_decimal function is broken and is overwriting by one character the array you pass into it. (Probably because your function assumes the inputs are MAXLENGTH long and you are creating MAXLENGTH-1 instead.)

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    thanks Tabstop...you helped me look to where the problem was....it was my remove_decimal function....I was using pointers in that function assuming that my declared arrays there would not allow me to go past their size. To test, I switched around the order of my array declarations in the main function, the original was:
    Code:
     char nd_base_num[MAXLENGTH - 1], nd_series_start_num[MAXLENGTH - 1];
    which I changed to
    Code:
    char nd_series_start_num[MAXLENGTH - 1], nd_base_num[MAXLENGTH - 1];
    when I tested the code I got all reverse results as originally posted so what that's telling me is that those array declarations are sitting beside each other in memory and its easy (if your not careful), to increment your pointer from the declared array your working on into the next one. I guess you could do some neat little 'tricks' with that, although I'm sure it wouldn't be reliable; sometimes maybe they don't sit besides each other and are " "wrapping in memory", or something like that.
    Thanks again Tabstop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM