Solved

variable-length array?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 199
GUI: DIalog Stacking and Popping in MS C++ 4 76
Need some help with Microsoft Visual Studio C++ 2003 5 60
Header of docx file 17 103
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 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