Solved

problem with declaration out of main()

Posted on 2008-10-25
10
266 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
 
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now