|
When I try to compile my code I get this error message:
@: gcc -o hw3 hw3.c
hw3.c: In function `main':
hw3.c:20: parse error before `char'
hw3.c:31: `buff' undeclared (first use in this function)
hw3.c:31: (Each undeclared identifier is reported only once
hw3.c:31: for each function it appears in.)
The error occurs because of my if statement.
When I place the if statement after the char declaration it works, also when I remove the if statement it compiles and works fine.
if(argc == 1){
printf("Error: hw3 <buffersize>");
return 1;
}
Also for a plain "if(){}" as well.
How do I get around this?
#include
#include
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
int fileread, filewrite;
ssize_t nread, nwrite;
if(argc == 1){
printf("Error: hw3 <buffersize>");
return 1;
}
char buff[ (int)argv[2] ];
fileread = open("128kB.bmp", O_RDONLY);
filewrite = creat("copy.bmp", O_WRONLY);
while(1)
{
nread = read(fileread, buff, sizeof(buff) );
if (nread == 0) break;
nwrite = write(filewrite, buff, sizeof(buff) - 1 );
}
close(fileread);
close(filewrite);
return 0;
}</buffersize></stdio.h></fcntl.h>
I am using a putty terminal connected to a unix machine.
|
|
|
|
|
Endomlic wrote: When I place the if statement after the char declaration it works
Then, why not do it that way?
I reckon good old C compilers expect you to declare all the variables used in a a function at the very beginning of the function block and not anywhere in between. I also think that more recent versions of C compilers allow this.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
If I declare the char beforehand then I can't use the if statement as intended.
I need to see if they actually entered a buffer size, if not then the program will crash with the wrong error message I want displayed.
That's where the if statement comes in. The if will break the program if they do not specify a buffer size. and if they do then it will be used as the char buff array size.
How else can I declare a char buffer size and change it after the if?
|
|
|
|
|
Endomlic wrote: How else can I declare a char buffer size and change it after the if?
Declaring it as a pointer variable prior to the if statement, and doing dynamic memory allocation later sounds like a viable option?
Or may be switch to a more recent version of the compiler that supports declaring variables anywhere.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Endomlic wrote: char buff[ (int)argv[2] ];
1. You are trying to create buffer dynamically. -Without specifying a constant value for memory allocation. It requires you to use "new".
2. Use atoi to convert argv[2] to an int value. You cannot just cast like that.
Example:
char* buff = new char[atoi(argv[2])];
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
VuNic wrote: char* buff = new char[atoi(argv[2])];
Dynamic buffer? "new"?
It is a C program and I see the issue is different. The OP is attempting to do something that his version of compiler doesn't support. I might be wrong though...
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Oops! "malloc" then. The idea is same in C right? How will it allow you to create buffers without knowing the size? But I'm not sure. Formatted C:\! from my brain. quite a while before
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
yea, how do I do it using malloc
|
|
|
|
|
char * buff = (char*)malloc(atoi(argv[2]));
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
I've tried that, but it acts as if it's not assigning buff with a size. in other words it's not working.
|
|
|
|
|
Endomlic wrote: but it acts as if it's not assigning buff with a size. in other words it's not working.
Are you really passing the Command-line argument? I think you are not. Command line args start from 0. If you are trying to mean the first argument, You must use argv[1] and not argv[2]. argv[0] the refers to the program name. Just try the below code to test how it allocates memory dynamically.
char size[]="10";
char * chBuff = (char*)malloc(sizeof(char)*atoi(size));
strcpy(chBuff,"TestMail");
for(int i=0;i<8;i++)
{
printf("%c",chBuff[i]);
}
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
VuNic wrote: How will it allow you to create buffers without knowing the size?
Some versions of the GNU C compilers allocate memory equivalent to the size of a medium grapefruit and hope for the best when the size requirement isn't exactly known.
OK, you know it's malloc, of course. Plus, there was the other problem which I stated in my reply to the OP.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
Yay I got it to work!
It was
char* buff;
if statement;
buff = (char*) malloc( sizeof(char) * atoi( argv[1] ) );
Apparently I had to use argv 1 instead of 2.
I guess in gcc argv 0 is the program name.
Thanks to you guys for all the help!!
|
|
|
|
|
I figured it out. Took few minutes to type this[^]. lol that's great you got it!
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
I guess in any C program argv[0] is the program name...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Endomlic wrote: I guess in gcc argv 0 is the program name.
Not just in gcc, it's the same everywhere. good night!
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
I missed this one.
Thanks to an anonymous report (or 'signalation?' ) it is now in the list it deserves...[^]
Welcome again in the CP's memorable quotes page, Rajesh.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
This should never compile. You must declare arrays with literals or assign them so the complier can calculate a size. When you create this array the size of the array * the sizeof(char) will be subtracted from the stack pointer. If you want a dynamic array you have to use malloc.
When you use a char * and a malloc the Size of buff is the size of a char * which will be 4 regardless of how much memory you allocate. In C you will need to maintain a separate value to indicate the size of the allocated array that is why every api function that takes an allocated buffer also takes a parameter that specifies the size of the buffer or a buffer that has a way of indicating the end element as is in a null terminator on the end of char strings.
a programmer traped in a thugs body
|
|
|
|
|
HI ..i want to convert a key board input to uppercase while typeing from keyboard.
how should i handle it..
Please provide comment
|
|
|
|
|
convert your input using toupper()
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
pandit84 wrote: i want to convert a key board input...
Are you using getch() ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I've been using static members inside class for years and I've been doing the traditional static-initialization outside the class too. But only when I'm in a poetic++ mode, I'd re-think about the practices.- Why should I really do it?? The compiler that's bending the world, can't be written to address this trivial requirement? Or it'd break any "conviction" made by Stroustroup? Why not it let people initialize a static member inside the constructor? Though it need not impact the memory allocation scheme, it would give the programmers a better feel..
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
When you refer to 'the constructor', do you mean a static constructor, like in C#? C++ doesn't have static constructors. I don't know if C+0x is going to support that or not (it might be nice if it did).
With an instance constructor, you can set static members just like instance members. Each instance of a class can modify a static member.
I'm not sure what you're saying is missing .
|
|
|
|
|
Gary R. Wheeler wrote: like in C#? C++ doesn't have static constructors. I don't know if C+0x is going to support that or not (it might be nice if it did).
Yeah something similar.
Gary R. Wheeler wrote: With an instance constructor, you can set static members just like instance members. Each instance of a class can modify a static member.
Yup, but for example:
class CodeProject
{
static int x;
int y;
CodeProject()
{
x=0;
y=0;
}
};
The above won't work without we initializing it again in the .cpp.(int CodeProject::x=0 ). In specific it must be in .cpp - it won't work if we initialize the static member in the header. Just thought about these limitations & felt it might be possible to disguise a static member as an instance member and treat it the same way. (though internal implementation would differ).
And yup, I wish C+0x have this feature unless they've been following this way for any specific reasons.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|