Solved

variable-length array?

Posted on 2002-03-06
4
247 Views
Last Modified: 2010-04-01
What's wrong with this short program, and how do I make it work? (I do not want to fix/limit the size of the array to 10000.)

Thanks.
---------------
#include <stdio.h>
#include <stdlib.h>

void main(int argc, char** argv){
//  char content[10000];
  if(argc<3){
     printf("Usage: \n       sendfile number_of_bytes file_name\n");
     exit(0);
  }

  FILE* fout=fopen(argv[2],"w");
  int num=atoi(argv[1]);
int * size=(int *)malloc((size_t)num);
char content[*size];
  for(int i=0; i<num; i++)content[i]='a';
  fwrite(content,1,num,fout);
free(size);
}
0
Comment
Question by:anothercto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
pagladasu earned 100 total points
ID: 6844788
char *content=(char *)malloc(num);
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6844803
#include <stdio.h>
#include <stdlib.h>


void main(int argc, char **argv)
{
    //  char content[10000];
    if(argc < 3)
    {
        printf("Usage: \n       sendfile number_of_bytes file_name\n");
        exit(0);
    }
    FILE * fout = fopen(argv[2], "w");

    if( fout == NULL )
        return;
   
    int       num = atoi(argv[1]);
    char *content = (char *)malloc(num*sizeof(int));

    if( content == NULL )
        return;
   
    for(int i = 0; i < num; i++)
        content[i] = 'a';
    fwrite(content, 1, num, fout);

    free(content);
    fclose(fout);
}

0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6844806
if you write in "pure" C, the code as following.


#include <stdio.h>
#include <stdlib.h>


void main(int argc, char **argv)
{
    FILE * fout;
    int    num, i;
    char *content;

   
    //  char content[10000];
    if(argc < 3)
    {
        printf("Usage: \n       sendfile number_of_bytes file_name\n");
        exit(0);
    }

    fout = fopen(argv[2], "w");
    if( fout == NULL )
        return;
   
    num = atoi(argv[1]);
    content = (char *)malloc(num*sizeof(int));

    if( content == NULL )
        return;
   
    for(i = 0; i < num; i++)
        content[i] = 'a';
    fwrite(content, 1, num, fout);

    free(content);
    fclose(fout);
}

0
 

Author Comment

by:anothercto
ID: 6844842
thienpnguyen,

I'll post 50 points for you.
Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question