473,466 Members | 4,879 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I return this struct pointer?

1 New Member
I am having trouble understanding why the compiler is giving me the following error:

Expand|Select|Wrap|Line Numbers
  1. level0.c: In function ‘create_grid’:
  2. level0.c:28:9: warning: return from incompatible pointer type [-Wincompatible-pointer-types]
  3.   return grid;
  4.  
I am simply trying to return a pointer to a struct that I created in the function.

The code for the function is:

Expand|Select|Wrap|Line Numbers
  1. struct gridType* create_grid(int length){
  2.  
  3.     char** array = malloc(length * sizeof(*array));
  4.     for(int i = 0; i < length; i++){
  5.         array[i] = malloc(length * sizeof(array));
  6.     }   
  7.  
  8.     for(int i = 0; i < length; i++){
  9.         for (int j = 0; j < length; j++){
  10.             array[i][j] = '-';
  11.         }   
  12.     }   
  13.  
  14.     struct gridType{
  15.         int length; 
  16.         char** array;
  17.     };
  18.  
  19.     struct gridType* grid = malloc(sizeof(struct gridType));
  20.  
  21.     grid->length = length;
  22.     grid->array = array;
  23.  
  24.     return grid;
  25. }
  26.  
Oct 22 '15 #1
2 1201
weaknessforcats
9,208 Recognized Expert Moderator Expert
C is sloppy. It makes assumptions when it shouldn't. One of these occurs when you use a pointer to a type before you define it.

Your solution here is to move all
Expand|Select|Wrap|Line Numbers
  1. struct gridType{
  2.     int length;
  3.     char** array;
  4. };
from inside your functions to the top of the file and outside any function. That way gridType is clearly declared and no assumptions will be made by the compiler.
Oct 23 '15 #2
donbock
2,426 Recognized Expert Top Contributor
@weaknessforcat's advice should get rid of the error message.

You should verify that malloc didn't fail before dereferencing the pointer it returns.
You should also decide what your function will return when malloc fails (NULL is a good choice).

By the way, if it should turn out that users of create_grid() don't ever directly access the structure fields then you have the option of providing an "incomplete definition" of the structure to users of create_grid().
Expand|Select|Wrap|Line Numbers
  1. struct gridType;
This is enough to declare pointers to grid_type. You only need the complete structure definition where you access structure fields or allocate memory to hold the structure.
Oct 23 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bryan Parkoff | last post by:
I set Function Pointer variable to private so unauthorized users cannot access it, but they are allowed to use Get_ and Set_ functions to modify Function Pointer variable. Look at my example. ...
7
by: Serve Laurijssen | last post by:
How can I get the following to compile? typedef struct Y { void (*f)(X *x); } Y; typedef struct X { int x;
7
by: Jim Showalter | last post by:
I always thought that it is safe for a function to return a pointer to static storage. And the following code does compile quietly with: gcc -pedantic -Wall -o foo foo.c #include <stdio.h> ...
19
by: Sergey Koveshnikov | last post by:
Hello, If my function return a pointer on a memory area, where do I free it? e.x.: char *foo() { char *p; p = malloc(10); strcpy(p, "something"); return(p); }
4
by: Ole | last post by:
hello, Little problem: struct operatable { char * operatable_id; int ( * delegate ) ( ... ); somedatatype data; };
5
by: Mark Feller | last post by:
I have an embedded application where malloc( ) is not available. So I declare images as globals, but want to be able to pass image information including a pointer in a structure, as in the...
3
by: BJT | last post by:
Hello, How can I return a struct type e.g. System::Drawing::Point from a managed c++ class to a C# assembly? When I have the declaration of the managed C++ class's member function as...
9
by: decker | last post by:
People, In the below code, I would like to know how I can return correctly the two-dim array. Anyone can help me? /*************/ int **ReturnIntPointer(int **array); int main(){ int...
6
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.