Find the answer to your Linux question:
Results 1 to 6 of 6
i'm practicing in very basic c programs using the gcc compiler.I found that when i create two variables let's say Code: int a,b and Code: a=15; b=3; a=b; b--; then ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    19

    [SOLVED] c programming with gcc: variables?


    i'm practicing in very basic c programs using the gcc compiler.I found that when i create two variables let's say
    Code:
    int a,b
    and
    Code:
    a=15;
    b=3;
    a=b;
    b--;
    then a equals 2.I thought that this isn't normal in C isn't it?
    I haven't had the time to read the gcc documentation yet...so i think it has something to do with my compiler's default settings.I use the
    Code:
    gcc filename.c -o filename
    command to compile.Are all variables defined like pointers?

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Please give the complete program. Then we will find the mistake better.

    > Are all variables defined like pointers?
    No.
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just Joined!
    Join Date
    Aug 2010
    Posts
    19
    well here it is...its supposed to make an histogram with the lengths of the words that it gets from the input

    Code:
    #include <stdio.h>
    #define IN 1
    #define OUT 0
    #define MAXN 15
    
    int main(void){
    
        int wl,a[MAXN],i,j,state;
        int f1,f2;
            
        for(i=0;i<MAXN;i++) a[i]=0;
        state=OUT;wl=0;
        while((i=getchar())!=EOF){
            if((i=='\n') || (i=='\t') || (i==' ')){
                if(wl!=0) a[wl]++; 
                state=OUT;
                wl=0;
                }
            else state=IN;
            if(state==IN) wl++;
            }
        
        f1=1;                                                   //f1 is the index and f2 is the max value
        f2=a[1];
        for(i=1;i<MAXN+1;i++){
            if(f2<a[i]){
                 f2=a[i];
                 f1=i;
            }
        }
    
        for(j=f2;j>0;j--){    
            for(i=1;i<MAXN+1;i++){
                if(j==a[i]) {
                    printf("* ");
                    a[i]--;
                }
                else printf("  ");
                }
        }
    
        printf("\n");
        
        for(i=1;i<MAXN+1;i++) {
            if(i<10) printf("--");
            else printf("---");
            }
        printf("\n");
        for(i=1;i<MAXN+1;i++) printf("%d ",i);
        printf("\nthe max value was %d found in column %d\n",f2,f1);
        
        
        return 0;
    }

  4. #4
    Just Joined!
    Join Date
    Aug 2010
    Posts
    19
    The problem is that when f2 becomes 0 although its supposed to have a value >0.

    i found that
    1) if i replace
    Code:
    for(j=f2;j>0;j--){...}
    with
    Code:
    j=f2;
    for(;j>0;j--){...}
    the value of f2 remains intact.

    2) if i define f2 as
    Code:
    extern int f2;
    f2 retains its value.

    any ideas?

  5. #5
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    I think you have one or more buffer overflows. Maybe, if you use the first style, the memory for f2 is allocated right behind the array. Hence its values is changed when you don't expect it.

    Let's look at it closely.
    You define:
    Code:
    int a[15];
    The fist element is a[0], and a[14] is the last one.

    Later you write several times, basicallly:
    Code:
    for(i=1;i<15+1;i++){
            if(f2<a[i]){
                 f2=a[i];
                 f1=i;
            }
        }
    This means you start working on a[1] and finish at a[15], which is one integer beyond the edge (a[14]). I suspect it is that place where f2 is stored.
    If you use extern, the f2 is stored somewhere else in memory and you get away with the error you made (for now).
    Debian GNU/Linux -- You know you want it.

  6. #6
    Just Joined!
    Join Date
    Aug 2010
    Posts
    19
    great! thanks! i thought it had something to do with memory allocation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •