• C

problem with declaration out of main()

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

akohanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peetmCommented:
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
Kent OlsenData Warehouse Architect / DBACommented:

>> 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
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

peetmCommented:
>>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
peetmCommented:
And now it's a

void *

Which is 'right' - as far as it goes [it pacify]
0
HooKooDooKuCommented:
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
MysidiaCommented:
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
Kent OlsenData Warehouse Architect / DBACommented:
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
peetmCommented:
>>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
akohanAuthor Commented:


Thanks to all for your help.

Regards.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.