Well, I am trying to apply this to my main program below, and I am still getting this particular type of warning when trying to return an array of pointers. Where does the problem lie in this situation? Thank you.
Code:
main.c|149|warning: return from incompatible pointer type [enabled by default]|
Code:
// getlinks: get URLs from a forum
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define URL_LENGTH 150
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp);
char ** GetLinks(struct MemoryStruct datastruct);
int main(void)
{
curl_global_init(CURL_GLOBAL_ALL);
CURL *myhandle;
CURLcode result;
struct MemoryStruct datastruct;
datastruct.memory = malloc(1); // will be grown as needed by realloc
datastruct.size = 0; // no data at this point
if((myhandle = curl_easy_init()) == NULL)
{
perror("curl_easy-init error");
exit(EXIT_FAILURE);
}
if((result = curl_easy_setopt(myhandle, CURLOPT_URL, "http://cboard.cprogramming.com/c-programming/")) != CURLE_OK)
{
perror("CURLOP_URL error");
exit(EXIT_FAILURE);
}
if((result = curl_easy_setopt(myhandle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback)) != CURLE_OK)
{
perror("CURLOPT_WRITEFUNCTION error");
exit(EXIT_FAILURE);
}
if((result = curl_easy_setopt(myhandle, CURLOPT_WRITEDATA, &datastruct)) != CURLE_OK)
{
perror("CURLOPT_WRITEDATA error");
exit(EXIT_FAILURE);
}
if((result = curl_easy_perform(myhandle)) != 0)
{
perror("curl_easy_perform error");
exit(EXIT_FAILURE);
}
curl_easy_cleanup(myhandle);
GetLinks(datastruct);
return 0;
}
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL) /* out of memory! */
{
printf("not enough memory (realloc returned NULL)\n");
exit(EXIT_FAILURE);
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
char ** GetLinks(struct MemoryStruct datastruct)
{
char * temp_string;
char * memory_copy;
char * match; // pointer to search match;
int location; // location of search match;
static char ** links[100];
char *search_string = "http://cboard.cprogramming.com/c-programming/";
int ch; // test character
int array_ctr = 0, len_memory_string, len_search_string, len_temp_string, ctr = 0, url_buffer_ctr = 0;
len_memory_string = strlen(datastruct.memory);
if((memory_copy = malloc(len_memory_string * sizeof(char) + 1)) == NULL)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
strcpy(memory_copy, datastruct.memory);
len_search_string = strlen(search_string);
while((match = strstr(memory_copy, search_string)) != NULL)
{
location = match - memory_copy;
len_temp_string = strlen(datastruct.memory) - (location + len_search_string + 1); // +1 to include " char
if((ch = datastruct.memory[location + len_search_string]) == '"')
{
if((temp_string = malloc(len_temp_string * sizeof(char) + 1)) == NULL)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
strcpy(temp_string, &datastruct.memory[location + len_search_string + 2]); // +2 to move past the " char
if((memory_copy = realloc(memory_copy, len_temp_string + 1)) == NULL)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
strcpy(memory_copy, temp_string);
continue;
}
else
{
while((ch = memory_copy[location]) != '"')
{
if((links[array_ctr] = malloc(URL_LENGTH * sizeof(char))) == NULL) // allocate memory for pointer to hold URL
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
*links[array_ctr][url_buffer_ctr] = ch;
location++;
url_buffer_ctr++;
}
links[array_ctr][url_buffer_ctr] = '\0';
puts(*links[0]);
array_ctr++; // increment array to accept new URL
url_buffer_ctr = 0; // reset
location++; // move past "
len_temp_string = strlen(memory_copy) - location; // the rest of memory_copy after the "
if((temp_string = realloc(temp_string, len_temp_string + 1)) == NULL)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
strcpy(temp_string, &memory_copy[location]);
if((memory_copy = realloc(memory_copy, len_temp_string + 1)) == NULL)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
strcpy(memory_copy, temp_string);
continue;
}
free(memory_copy);
free(temp_string);
return links;
}
}