Solved

variable-length array?

Posted on 2002-03-06
4
211 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
  • 2
4 Comments
 
LVL 4

Accepted Solution

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

Expert Comment

by:thienpnguyen
Comment Utility
#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
Comment Utility
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
Comment Utility
thienpnguyen,

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now