Solved

problem with declaration out of main()

Posted on 2008-10-25
10
267 Views
Last Modified: 2012-05-05
Hello group,

I following code I have defined a char** as a global element but I keep getting error message as :

error: initializer element is not constant

Why is that and how can I fix it?

Regards.

#include files ...
 
char** input = malloc(SomeSize)
 
main(){
 
}

Open in new window

0
Comment
Question by:akohan
10 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
ID: 22805705
Hi akohan,

malloc() is an executable function.  I must be called from within another function.

The example below should work just fine.



Good Luck,
Kent

#include files ...
 
char** input;
 
main(){
 
  input = (char**) = malloc(SomeSize);
 
}

Open in new window

0
 
LVL 10

Expert Comment

by:peetm
ID: 22805740
Ok, 1st.  Globals in C need to be initialised with a constant - which you're not doing.

And ...

Well, you've omitted quite a lot but ... you just can't do that in C [as above] have a global contain the result of a runtime operation like the result of a malloc() call.

malloc() returns a pointer - to either to a char or to a void; depending upon your implementation.  I.e., a char * or a void *.

You're specifying that 'input' is of type char **, i.e., a pointer to a pointer to char.

So, what is it that you're really trying to do here?  Set a global at runtime, or what?

0
 
LVL 45

Expert Comment

by:Kdo
ID: 22805748

>> malloc() returns a pointer - to either to a char or to a void

Nope.  malloc returns a pointer to an address.  The data type of the returned value is void*.  

Some compiler options will let you assign a void* to another pointer type (like a char*) without issuing an error or warning.  But the pointer returned by malloc has a type of void*.


Kent
0
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.

 
LVL 10

Expert Comment

by:peetm
ID: 22805860
>>malloc() returns a pointer to an address.

Yup - got me there!

And, far too much wine!  I should NOT look at this site when I'm feeling 'cocky'!

OH ..........

>>The data type of the returned value is void*.  

Um, what's the standard you're refering to?

When I was on the X3J11 std, this was a char *
0
 
LVL 10

Expert Comment

by:peetm
ID: 22805869
And now it's a

void *

Which is 'right' - as far as it goes [it pacify]
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22805874
Kdo ALMOST has it like it's needed.
There are two things I would change with Kdo's solution.  First of all, I would initialize the global variable with NULL so that if the very first line of code executed is not the malloc, you've got the variable initialized to NULL as a flag that it's never been malloc()ed.

Second, I thing the malloc line had a typo with an extra '='.
#include files ...
 
char** input = NULL;
 
main(){
   input = (char**) malloc(SomeSize);
 
}

Open in new window

0
 
LVL 23

Expert Comment

by:Mysidia
ID: 22806201
You cannot initialize a global variable to the result of a function call, although you may do this

char** input;
main(){
    input = (char**)malloc(someSize);
 
}



Today's void*  is yesterday's  char*   in more ways than malloc return values.
The extended usage of "void*" in place of char* is an ANSI C thing.   There are still implementations of C where malloc returns a 'char *';  they are just not ANSI/ISO C,  and they are not all that popular.

But it would still be wrong to assume either way, as legacy implementations are still in use by many C programmers.

New or old, ANSI C or not though, these are not "pointers to an address";  they are pointers to the lowest addressed byte of an object created by the call to malloc(); if it was successful,  otherwise the  null pointer.


There is not formally such an object n C as a "pointer to address", if you go by the language spec, although there are 'pointers to pointers'  and these may be informally called a 'pointer to an address'.

In fact the term 'pointer to an address'  is ambiguous. At a low level all pointers refer to an "address";   some programmers mistakenly say 'pointer to an address' when they should just say 'pointer'.

Something that is a pointer to a pointer (or often stated by C programmers as: pointer to an address) implies double indirection.  

they look like
void **ptr;

In the case of

ptr = (void**)malloc(mysize);
*ptr = malloc(10);

Though,  malloc is still merely returning a pointer to the first byte  of the newly allocated region.

It is just that you (the programmer)  are then choosing to use part of the newly allocated object to hold yet another pointer.

Nothing forced that on you.
With void types and proper casting of elements  to char*,  you could have stored character data in the various bytes of the 'ptr'    object   instead of  actually storing a pointer-to-a-pointer in it.




0
 
LVL 45

Expert Comment

by:Kdo
ID: 22812279
Wow...   A lot of discussion for such a simple issue.   :)

Hi Pete,

I travelled all weekend, and as the designated driver didn't get a chance to imbibe.  I hope that you had one for me.  :)


Hi HooKoo,

Good catch on the second '='.  I looked twice before seeing it.

As far as initializing the global variable to NULL, it's probably good practice from a "documentation" standpoint, but unnecessary from a coding standpoint.  Being in the globals block it will be initialized to 0 when the application starts.


Kent
0
 
LVL 10

Expert Comment

by:peetm
ID: 22812787
>>I travelled all weekend, and as the designated driver didn't get a chance to imbibe.  I hope that you had one for me.  :)

I actually had two for you Kent!
0
 

Author Comment

by:akohan
ID: 22825620


Thanks to all for your help.

Regards.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

803 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