Solved

variable-length array?

Posted on 2002-03-06
4
242 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Focus not getting shifted out of  editbox 2 69
object oriented programming on screen browser tutorial lesson 2 87
Dialogbox API leak? 18 109
c++, dynamic object by json 1 60
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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.

749 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