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 ...
- 09-13-2010 #1Just 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
andCode:int a,b
then a equals 2.I thought that this isn't normal in C isn't it?Code:a=15; b=3; a=b; b--;
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
command to compile.Are all variables defined like pointers?Code:gcc filename.c -o filename
- 09-13-2010 #2
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.
- 09-13-2010 #3Just 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; }
- 09-13-2010 #4Just 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
withCode:for(j=f2;j>0;j--){...}
the value of f2 remains intact.Code:j=f2; for(;j>0;j--){...}
2) if i define f2 as
f2 retains its value.Code:extern int f2;
any ideas?
- 09-13-2010 #5
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:
The fist element is a[0], and a[14] is the last one.Code:int a[15];
Later you write several times, basicallly:
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.Code:for(i=1;i<15+1;i++){ if(f2<a[i]){ f2=a[i]; f1=i; } }
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.
- 09-13-2010 #6Just Joined!
- Join Date
- Aug 2010
- Posts
- 19
great! thanks! i thought it had something to do with memory allocation.


