Skip to contentSkip to conversation

Variable to sizeof function.

DISCUSSION Vikas S 7 participants 12 messages Jul 15, 2005 – Aug 1, 2005

lore
Share & tools
lore

Vikas S

author
#0 lore raw

Hi,

I want to find out the size of a structure which the user will give as an argument as follows.

#include <> -- All includes..
..
main(int arc, char *argv[])
{
printf("Size of structure %s is: %d\n", argv[1], sizeof(struct argv[1])); }

However, I'm unable to compile. It's giving:
"sizeof applied to an incomplete type".

How can I get it working.

Thanks,
Vikas

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

#1 lore raw

Vikas,

Quoted 6 lines · Vikas S

I want to find out the size of a structure which the user will give as an argument as follows.

#include <> -- All includes..
..
main(int arc, char *argv[])

argv is a charecter pointer . You cannot pass struct * as an
arguement to main.

Quoted 2 lines

{
printf("Size of structure %s is: %d\n", argv[1], sizeof(struct argv[1]));

use strlen(argv[1]) to find the lenght of the string.

Signature · 2 lines
cheers,
Vadi
#2 lore raw

strlen on a null pointer will segfault...
if arc is 1 then argv[0] will contain a pointer to an array of char if arc is 2 then argv[1] (the users first argument) will contain an array of char..
so don't forget to check the number of arguments ..

Quoted 26 lines · Vadiraj

Vikas,

Quoted 6 lines · Vikas S

I want to find out the size of a structure which the user will give as an argument as follows.

#include <> -- All includes..
..
main(int arc, char *argv[])

argv is a charecter pointer . You cannot pass struct * as an
arguement to main.

Quoted 2 lines

{
printf("Size of structure %s is: %d\n", argv[1], sizeof(struct argv[1]));

use strlen(argv[1]) to find the lenght of the string.

--
cheers,
Vadi
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#3 lore raw

I guess I was not clear. Say, we have two structures.

str1 {int x, char y} and str2 {int x, char *y};

printf(sizeof(struct str1)); will give proper output. ie memory occupied by str1.

What I want is, instead of hard-coding str1 etc., I want to find the size of structure which I will give as 1st argument. So, if the program name is size, i'll give:

$ ./size str1 --- to get size of str1
$ ./size str2 --- to get size of str2

The code which I gave earlier is giving compile-time error message.

Thanks,
Vikas

Quoted 33 lines · --- Rechberger Markus

strlen on a null pointer will segfault...
if arc is 1 then argv[0] will contain a pointer to an array of char if arc is 2 then argv[1] (the users first argument) will contain an array of char..
so don't forget to check the number of arguments ..

Quoted 18 lines · Vadiraj

Vikas,

Quoted 6 lines · Vikas S

I want to find out the size of a structure which the user will give as an argument as follows.

#include <> -- All includes..
..
main(int arc, char *argv[])

argv is a charecter pointer . You cannot pass struct * as an
arguement to main.

Quoted 2 lines

{
printf("Size of structure %s is: %d\n", argv[1], sizeof(struct argv[1]));

use strlen(argv[1]) to find the lenght of the string.

Quoted 8 lines

--
cheers,
Vadi
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail

#4 lore raw
Quoted 4 lines · Vikas S

I guess I was not clear. Say, we have two structures.

str1 {int x, char y} and str2 {int x, char *y};

OK...

Quoted 2 lines

printf(sizeof(struct str1)); will give proper output. ie memory occupied by str1.

...reasonable...

Quoted 5 lines

What I want is, instead of hard-coding str1 etc., I want to find the size of structure which I will give as 1st argument. So, if the program name is size, i'll give:

$ ./size str1 --- to get size of str1
$ ./size str2 --- to get size of str2

This makes things even more confusing to me. So, you want

(a) query the size of a particular structure among others
(b) provide a structure as an argument to your program

identified by name? Obviously (b) is something completely impossible, obscure at least. The solution to (a) is simple:

    struct s1 { int i; char *c; } str1;
    struct s2 { int i; char c[5]; } str2;

    if (argc != 2) return 0;

    if (!strcmp(argv[1], "str1"))
        printf("sizeof(str1): %d\n", sizeof(str1));
    else
        printf("sizeof(str2): %d\n", sizeof(str2))
$ cc -o test test.c
$ ./test str2
sizeof(str2): 12
Quoted 58 lines

The code which I gave earlier is giving compile-time error message.

Thanks,
Vikas

Quoted 33 lines · --- Rechberger Markus

strlen on a null pointer will segfault...
if arc is 1 then argv[0] will contain a pointer to an array of char if arc is 2 then argv[1] (the users first argument) will contain an array of char..
so don't forget to check the number of arguments ..

Quoted 18 lines · Vadiraj

Vikas,

Quoted 6 lines · Vikas S

I want to find out the size of a structure which the user will give as an argument as follows.

#include <> -- All includes..
..
main(int arc, char *argv[])

argv is a charecter pointer . You cannot pass struct * as an
arguement to main.

Quoted 2 lines

{
printf("Size of structure %s is: %d\n", argv[1], sizeof(struct argv[1]));

use strlen(argv[1]) to find the lenght of the string.

Quoted 8 lines

--
cheers,
Vadi
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 6 lines
                                 ______________________________________
Steve Graegert                  //
Software Consultancy           //    Whether you know it or not, if you
Mobile: +49 (176)  21248869   // are a hacker, you are a revolutionary.   
Office: +49 (9131) 7126409   //  Don't worry, you're on the right side.
____________________________//         -- Dr Crash / Phrack 6 / phile 3
author
#5 lore raw

Thanks, This is a workable solution.
But the issue is I've some 100+ structures. So, writing a if or case is going to be difficult.

a) You are right.
b) Yes. I want to pass a structure name. ie, make sizeof accept a variable.

Is this impossible?

Thanks,
Vikas

Quoted 92 lines · --- Steve Graegert
Quoted 4 lines · Vikas S

I guess I was not clear. Say, we have two structures.

str1 {int x, char y} and str2 {int x, char *y};

OK...

Quoted 2 lines

printf(sizeof(struct str1)); will give proper output. ie memory occupied by str1.

...reasonable...

Quoted 5 lines

What I want is, instead of hard-coding str1 etc., I want to find the size of structure which I will give as 1st argument. So, if the program name is size, i'll give:

$ ./size str1 --- to get size of str1
$ ./size str2 --- to get size of str2

This makes things even more confusing to me. So, you want

(a) query the size of a particular structure among others
(b) provide a structure as an argument to your program

identified by name? Obviously (b) is something completely impossible, obscure at least. The solution to (a) is simple:

    struct s1 { int i; char *c; } str1;
    struct s2 { int i; char c[5]; } str2;

    if (argc != 2) return 0;

    if (!strcmp(argv[1], "str1"))
        printf("sizeof(str1): %d\n", sizeof(str1));
    else
        printf("sizeof(str2): %d\n", sizeof(str2))
$ cc -o test test.c
$ ./test str2
sizeof(str2): 12
Quoted 49 lines

The code which I gave earlier is giving compile-time error message.

Thanks,
Vikas

Quoted 38 lines · --- Rechberger Markus

strlen on a null pointer will segfault...
if arc is 1 then argv[0] will contain a pointer to an array of char if arc is 2 then argv[1] (the users first argument) will contain an array of char..
so don't forget to check the number of arguments ..

Quoted 18 lines · Vadiraj

Vikas,

Quoted 6 lines · Vikas S

I want to find out the size of a structure which the user will give as an argument as follows.

#include <> -- All includes..
..
main(int arc, char *argv[])

argv is a charecter pointer . You cannot pass struct * as an
arguement to main.

Quoted 2 lines

{
printf("Size of structure %s is: %d\n", argv[1], sizeof(struct argv[1]));

use strlen(argv[1]) to find the lenght of the string.

Quoted 8 lines

--
cheers,
Vadi
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 23 lines
Quoted 9 lines

__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 10 lines
                                 ______________________________________
Steve Graegert                  //
Software Consultancy           //    Whether you know it or not, if you
Mobile: +49 (176)  21248869   // are a hacker, you are a revolutionary.   
Office: +49 (9131) 7126409   //  Don't worry, you're on the right side.
____________________________//         -- Dr Crash / Phrack 6 / phile 3
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

#6 lore raw
Quoted 4 lines · Vikas S

Thanks, This is a workable solution.
But the issue is I've some 100+ structures. So, writing a if or case is going to be difficult.

Wow, that's a lot of work :-)

Quoted 5 lines

a) You are right.
b) Yes. I want to pass a structure name. ie, make sizeof accept a variable.

Is this impossible?

I guess you want to save typing and be able to query the size of a particular structure dynamically simply by specifying its name (the variable name) on the command line. Unfortunately there is no mechanism known to me that allows it. The C programming language provides no way to do some name to variable mapping. A couple of days ago, one has asked how to obtain the number and names of function arguments dynamically which is a problem of the same kind. What you are looking for is some kind of reflection to fetch type information at run-time.

You can try to set up a table with pointers to every structure and use this entry to query the size. I don't see another solution.

Regards

\Steve

Quoted 127 lines

Thanks,
Vikas

Quoted 105 lines · --- Steve Graegert
Quoted 4 lines · Vikas S

I guess I was not clear. Say, we have two structures.

str1 {int x, char y} and str2 {int x, char *y};

OK...

Quoted 2 lines

printf(sizeof(struct str1)); will give proper output. ie memory occupied by str1.

...reasonable...

Quoted 5 lines

What I want is, instead of hard-coding str1 etc., I want to find the size of structure which I will give as 1st argument. So, if the program name is size, i'll give:

$ ./size str1 --- to get size of str1
$ ./size str2 --- to get size of str2

This makes things even more confusing to me. So, you want

(a) query the size of a particular structure among others
(b) provide a structure as an argument to your program

identified by name? Obviously (b) is something completely impossible, obscure at least. The solution to (a) is simple:

    struct s1 { int i; char *c; } str1;
    struct s2 { int i; char c[5]; } str2;

    if (argc != 2) return 0;

    if (!strcmp(argv[1], "str1"))
        printf("sizeof(str1): %d\n", sizeof(str1));
    else
        printf("sizeof(str2): %d\n", sizeof(str2))
$ cc -o test test.c
$ ./test str2
sizeof(str2): 12
Quoted 49 lines

The code which I gave earlier is giving compile-time error message.

Thanks,
Vikas

Quoted 33 lines · --- Rechberger Markus

strlen on a null pointer will segfault...
if arc is 1 then argv[0] will contain a pointer to an array of char if arc is 2 then argv[1] (the users first argument) will contain an array of char..
so don't forget to check the number of arguments ..

Quoted 18 lines · Vadiraj

Vikas,

Quoted 6 lines · Vikas S

I want to find out the size of a structure which the user will give as an argument as follows.

#include <> -- All includes..
..
main(int arc, char *argv[])

argv is a charecter pointer . You cannot pass struct * as an
arguement to main.

Quoted 2 lines

{
printf("Size of structure %s is: %d\n", argv[1], sizeof(struct argv[1]));

use strlen(argv[1]) to find the lenght of the string.

Quoted 8 lines

--
cheers,
Vadi
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 9 lines

__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--

                                 ______________________________________
Quoted 10 lines

Steve Graegert //
Software Consultancy // Whether you know it or not, if you
Mobile: +49 (176) 21248869 // are a hacker, you are a revolutionary.
Office: +49 (9131) 7126409 // Don't worry, you're on the right side.
____________________________// -- Dr Crash / Phrack 6 / phile 3
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Signature · 6 lines
                                 ______________________________________
Steve Graegert                  //
Software Consultancy           //    Whether you know it or not, if you
Mobile: +49 (176)  21248869   // are a hacker, you are a revolutionary.   
Office: +49 (9131) 7126409   //  Don't worry, you're on the right side.
____________________________//         -- Dr Crash / Phrack 6 / phile 3
#7 lore raw
In reply to Vikas S
Quoted 6 lines

Thanks, This is a workable solution.
But the issue is I've some 100+ structures. So, writing a if or case is going to be difficult.

b) Yes. I want to pass a structure name. ie, make sizeof accept a variable.

You can't do this in C, but you might be able to achieve something similar using gdb, if performance isn't important. If your application is compiled with debug symbols, you can go ``inspect sizeof(struct my_struct)'' from the gdb prompt, and get the number you're looking for; you may be able to automate this.

If your application doesn't have debug symbols, then the information you need isn't preserved by the compiler, so you're stuffed.

Steven.

#8 lore raw
In reply to Vikas S

Vika,

Perhaps you can add a sizeof field to your structures and query it?

struct s{
int a;
float b;
int mysize = sizeof(s);
};

int function(struct *s){
return s->mysize;
}

Pardon my code, its more like psuedocode than anything that will compile, but the idea is there. The problem is if you have pointers to strings in your struct, then it won't get the size properly at run-time, but if they're all static elements, it may work.

Quoted 133 lines · Vikas S

Thanks, This is a workable solution.
But the issue is I've some 100+ structures. So, writing a if or case is going to be difficult.

a) You are right.
b) Yes. I want to pass a structure name. ie, make sizeof accept a variable.

Is this impossible?

Thanks,
Vikas

Quoted 100 lines · --- Steve Graegert
Quoted 3 lines · Vikas S

I guess I was not clear. Say, we have two structures.

str1 {int x, char y} and str2 {int x, char *y};

OK...

Quoted 2 lines

printf(sizeof(struct str1)); will give proper output. ie memory occupied by str1.

...reasonable...

Quoted 5 lines

What I want is, instead of hard-coding str1 etc., I want to find the size of structure which I will give as 1st argument. So, if the program name is size, i'll give:

$ ./size str1 --- to get size of str1
$ ./size str2 --- to get size of str2

This makes things even more confusing to me. So, you want

(a) query the size of a particular structure among others
(b) provide a structure as an argument to your program

identified by name? Obviously (b) is something completely impossible, obscure at least. The solution to (a) is simple:

    struct s1 { int i; char *c; } str1;
    struct s2 { int i; char c[5]; } str2;

    if (argc != 2) return 0;

    if (!strcmp(argv[1], "str1"))
        printf("sizeof(str1): %d\n", sizeof(str1));
    else
        printf("sizeof(str2): %d\n", sizeof(str2))
$ cc -o test test.c
$ ./test str2
sizeof(str2): 12
Quoted 47 lines

The code which I gave earlier is giving compile-time error message.

Thanks,
Vikas

Quoted 35 lines · --- Rechberger Markus

strlen on a null pointer will segfault...
if arc is 1 then argv[0] will contain a pointer to an array of char if arc is 2 then argv[1] (the users first argument) will contain an array of char..
so don't forget to check the number of arguments ..

Quoted 19 lines · Vadiraj

Vikas,

Quoted 6 lines · Vikas S

I want to find out the size of a structure which the user will give as an argument as follows.

#include <> -- All includes..
..
main(int arc, char *argv[])

argv is a charecter pointer . You cannot pass struct * as an
arguement to main.

Quoted 3 lines

{
printf("Size of structure %s is: %d\n", argv[1], sizeof(struct argv[1]));

use strlen(argv[1]) to find the lenght of the string.

Quoted 8 lines

--
cheers,
Vadi
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 9 lines

__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--

                                 ______________________________________
Quoted 10 lines

Steve Graegert //
Software Consultancy // Whether you know it or not, if you
Mobile: +49 (176) 21248869 // are a hacker, you are a revolutionary.
Office: +49 (9131) 7126409 // Don't worry, you're on the right side.
____________________________// -- Dr Crash / Phrack 6 / phile 3
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
----------------------------------------
--EB
Quoted 4 lines

All is fine except that I can reliably "oops" it simply by trying to read from /proc/apm (e.g. cat /proc/apm).
oops output and ksymoops-2.3.4 output is attached.
Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and a ballistic missile.

                --Alan Cox LKML-December 08,2000

----------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw
In reply to Vikas S
Quoted 4 lines · Vikas S

Thanks, This is a workable solution.
But the issue is I've some 100+ structures. So, writing a if or case is going to be difficult.

Another way I can suggest would be passing the file name at command prompt.
./size foo.txt

foo.txt should contain 100+ structure names.

read the file contents and follow steve's suggestion.

Quoted 138 lines

a) You are right.
b) Yes. I want to pass a structure name. ie, make sizeof accept a variable.

Is this impossible?

Thanks,
Vikas

Quoted 105 lines · --- Steve Graegert
Quoted 4 lines · Vikas S

I guess I was not clear. Say, we have two structures.

str1 {int x, char y} and str2 {int x, char *y};

OK...

Quoted 2 lines

printf(sizeof(struct str1)); will give proper output. ie memory occupied by str1.

...reasonable...

Quoted 5 lines

What I want is, instead of hard-coding str1 etc., I want to find the size of structure which I will give as 1st argument. So, if the program name is size, i'll give:

$ ./size str1 --- to get size of str1
$ ./size str2 --- to get size of str2

This makes things even more confusing to me. So, you want

(a) query the size of a particular structure among others
(b) provide a structure as an argument to your program

identified by name? Obviously (b) is something completely impossible, obscure at least. The solution to (a) is simple:

    struct s1 { int i; char *c; } str1;
    struct s2 { int i; char c[5]; } str2;

    if (argc != 2) return 0;

    if (!strcmp(argv[1], "str1"))
        printf("sizeof(str1): %d\n", sizeof(str1));
    else
        printf("sizeof(str2): %d\n", sizeof(str2))
$ cc -o test test.c
$ ./test str2
sizeof(str2): 12
Quoted 49 lines

The code which I gave earlier is giving compile-time error message.

Thanks,
Vikas

Quoted 33 lines · --- Rechberger Markus

strlen on a null pointer will segfault...
if arc is 1 then argv[0] will contain a pointer to an array of char if arc is 2 then argv[1] (the users first argument) will contain an array of char..
so don't forget to check the number of arguments ..

Quoted 18 lines · Vadiraj

Vikas,

Quoted 6 lines · Vikas S

I want to find out the size of a structure which the user will give as an argument as follows.

#include <> -- All includes..
..
main(int arc, char *argv[])

argv is a charecter pointer . You cannot pass struct * as an
arguement to main.

Quoted 2 lines

{
printf("Size of structure %s is: %d\n", argv[1], sizeof(struct argv[1]));

use strlen(argv[1]) to find the lenght of the string.

Quoted 8 lines

--
cheers,
Vadi
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 9 lines

__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--

                                 ______________________________________
Quoted 10 lines

Steve Graegert //
Software Consultancy // Whether you know it or not, if you
Mobile: +49 (176) 21248869 // are a hacker, you are a revolutionary.
Office: +49 (9131) 7126409 // Don't worry, you're on the right side.
____________________________// -- Dr Crash / Phrack 6 / phile 3
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
cheers,
Vadi
#10 lore raw

Query in C

Hello Everyone ,

Can anyone of you please tell me..

what does extern "C" int func(int *,foo) accomplish ?

waiting for your reply.

regards,
Avinash

#11 lore raw

Re: Query in C

Quoted 6 lines · avinash pawar

Hello Everyone ,

Can anyone of you please tell me..

what does extern "C" int func(int *,foo) accomplish ?

When using existing C headers that already conform to ANSI C in C++ they need to be slightly modified to enable proper linkage for the language and to ensure that C++ keywords are not used as identifiers.

This means that you can use C headers in C++ projects in the following way:

#ifdef __cplusplus
       extern "C" {

#endif
...
int func(int *,foo);
...

#ifdef __cplusplus
       }

#endif

Code placed between the two ifdefs is either linked as C or C++. The __cplusplus macro is usually predefined in appropriate system headers.

To answer your question: when a routine will be called from another language (here C is called from C++) it should be declared in C++ as

extern "C" int func(int *,foo);

The extern "C" causes the routine to have a so called 'unmangled' name that can be refered to not only from C but also from Fortran or Cobol.
Without extern "C" the routine will get a link 'mangled' link name
like func__abc.

Regards

\Steve

Signature · 4 lines
Steve Graegert
Software Consultancy
Mobile: +49 (176)  21248869   
Office: +49 (9131) 7126409