lal
lal

Reputation: 11

Why am I getting a “storage size isn't known” error in this C program?

This is my code:

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

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
char test[];
size_t  write_data(char *ptr, size_t size, size_t nmemb, FILE *stream)
{
  char buf[size*nmemb+1];
  char * pbuf = &buf[0];
  memset(buf, '\0', size*nmemb+1);
  size_t i = 0;
  for(;  i < nmemb ; i++){
    strncpy(pbuf,ptr,size);
    pbuf += size;
    ptr += size;
  }

  printf("%s",buf);
  test=new test[size*nmemb+1];
  return size * nmemb;
}

int main()
{
    CURL *curl_handle;
    curl_handle = curl_easy_init();

    curl_easy_setopt(curl_handle,   CURLOPT_URL, "http://www.google.com");
    curl_easy_setopt(curl_handle,   CURLOPT_NOPROGRESS  ,1);
    curl_easy_setopt(curl_handle,   CURLOPT_WRITEFUNCTION,&write_data);
    curl_easy_perform(curl_handle);
    curl_easy_cleanup(curl_handle);
    return 0;
}

Why I am getting this error:

../src/get_webpage.cpp:9: error: storage size of ‘test’ isn't known
../src/get_webpage.cpp: In function ‘size_t write_data(char*, size_t, size_t, FILE*)’:
../src/get_webpage.cpp:23: error: expected type-specifier before ‘test’
../src/get_webpage.cpp:23: error: expected ‘;’ before ‘test’

Upvotes: 1

Views: 5698

Answers (3)

user411313
user411313

Reputation: 3990

char buf[size*nmemb+1];

Is C99 not C89. For greater nmemb -> stackoverflow, better here also use a dyn. allocation

char *buf = calloc(1,size*nmemb+1), *pbuf = buf;
size_t i = 0;
if( !buf )
  { error-handling needed }
...
free(buf);

Upvotes: 0

Iustin
Iustin

Reputation: 1250

test=new test[size*nmemb+1];

This is C++ code, not C. Change it to

test = malloc(size*nmemb+1);

Also it is recommended to free it somewhere. Maybe at the end of main or before allocating it.

free(test);

Good luck.

Upvotes: 1

nos
nos

Reputation: 229184

Change char test[]; to char *test;

Upvotes: 2

Related Questions