Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. An 'If' statement before my char declaration is not allowing my program to compile......why?

An 'If' statement before my char declaration is not allowing my program to compile......why?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
19 Posts 5 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Endomlic

    yea, how do I do it using malloc :O

    E Offline
    E Offline
    Eytukan
    wrote on last edited by
    #9

    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

    E 1 Reply Last reply
    0
    • E Eytukan

      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 :rolleyes:

      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

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #10

      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. :laugh: 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

      E CPalliniC 2 Replies Last reply
      0
      • E Eytukan

        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

        E Offline
        E Offline
        Endomlic
        wrote on last edited by
        #11

        I've tried that, but it acts as if it's not assigning buff with a size. in other words it's not working.

        E 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          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. :laugh: 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

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #12

          :laugh: :laugh:

          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

          E 1 Reply Last reply
          0
          • E Eytukan

            :laugh: :laugh:

            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

            E Offline
            E Offline
            Endomlic
            wrote on last edited by
            #13

            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!!

            E CPalliniC 3 Replies Last reply
            0
            • E Endomlic

              I've tried that, but it acts as if it's not assigning buff with a size. in other words it's not working.

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #14

              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

              1 Reply Last reply
              0
              • E Endomlic

                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!!

                E Offline
                E Offline
                Eytukan
                wrote on last edited by
                #15

                I figured it out. Took few minutes to type this[^]. lol that's great you got it! :thumbsup:

                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

                1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  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. :laugh: 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

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #16

                  :-D I missed this one. Thanks to an anonymous report (or 'signalation?' :laugh: ) 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]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • E Endomlic

                    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!!

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #17

                    I guess in any C program argv[0] is the program name... :rolleyes:

                    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]

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    • E Endomlic

                      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!!

                      E Offline
                      E Offline
                      Eytukan
                      wrote on last edited by
                      #18

                      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

                      1 Reply Last reply
                      0
                      • E Endomlic

                        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 "); return 1; } Also for a plain "if(){}" as well. How do I get around this?

                        #include
                        #include
                        #include
                        #include

                        int main(int argc, char * argv[])
                        {
                        //our file references and read and write counts
                        int fileread, filewrite;
                        ssize_t nread, nwrite;

                        if(argc == 1){
                        	printf("Error: hw3 ");
                        	return 1;
                        }
                        
                        //our buffer size is determined by the user
                        char buff\[ (int)argv\[2\] \];
                        
                        // open 128kB file for reading
                        // create if it doesn't exist copy.bmp for writing
                        fileread = open("128kB.bmp", O\_RDONLY);
                        filewrite = creat("copy.bmp", O\_WRONLY);
                        
                        //copy all of the contents of the file	
                        while(1)
                        {
                        	//read data from the input file and place in the buffer
                        	nread = read(fileread, buff, sizeof(buff) );
                        
                        	//if no information left to read, exit while loop
                        	if (nread == 0) break;
                        
                        	//write the data from the buffer into the output file
                        	nwrite = write(filewrite, buff, sizeof(buff) - 1 );
                        }
                        
                        //close the files
                        close(fileread);
                        close(filewrite);
                        
                        return 0;
                        

                        }

                        I am using a putty terminal connected to a unix machine.

                        K Offline
                        K Offline
                        ky_rerun
                        wrote on last edited by
                        #19

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups