473,790 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing pointer array to function

I have a struct:

typedef struct {
char **user_comments ;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
void func (char **table)
{
return;
}

int main (void)
{
vorbis_comment vc;
/* This part GCC doesn't like */

func (vc.user_commen ts);

return 0;
}

GCC (3.1) would say that
warning: passing arg 1 of 'func' from incompatible pointer type


But still my code in 'func' (not posted, above is an example) works correctly,
utilizing whole '**table'.

Now what I want to ask is that which is the legal way to make a call of this
kind? Or is it just a bug or "feature" of GCC?
I am compiling with a commandline:

gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2
Nov 14 '05 #1
11 2072

"Tatu Portin" <ax****@mbnet.f i> wrote in message
news:TE******** *******@read3.i net.fi...
I have a struct:

typedef struct {
char **user_comments ;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
void func (char **table)
{
return;
}

int main (void)
{
vorbis_comment vc;
/* This part GCC doesn't like */

func (vc.user_commen ts);

return 0;
}

GCC (3.1) would say that
warning: passing arg 1 of 'func' from incompatible pointer type
But still my code in 'func' (not posted,


Why not?
above is an example) works correctly,
utilizing whole '**table'.

Now what I want to ask is that which is the legal way to make a call of this kind? Or is it just a bug or "feature" of GCC?


We cannot tell you the correct form for calling a function whose
definition (at least declaration) that we cannot see. What is
the signature of 'func()'? Remember that an array is not a pointer,
and a pointer is not an array.

-Mike
Nov 14 '05 #2
Tatu Portin wrote:
I have a struct:

cat main.c typedef struct {
char** user_comments;
int* comment_wds;
int comments;
char* vendor;
} vorbis_comment;
void func(char** table) {
return;
}

int main(int argc, char* argv[]) {
vorbis_comment vc;

/* GCC doesn't like this part. */

func(vc.user_co mments);

return 0;
}
gcc -Wall -ansi -pedantic -ffast-math -O2 -o main main.c
gcc --version gcc (GCC) 3.4.1

GCC (3.1) would say that
>warning: passing arg 1 of 'func' from incompatible pointer type


It seems to compile just fine for me.
Nov 14 '05 #3
Tatu Portin wrote:
I have a struct:

typedef struct {
char **user_comments ;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
void func (char **table)
{
return;
}

int main (void)
{
vorbis_comment vc;
/* This part GCC doesn't like */

func (vc.user_commen ts);

return 0;
}

GCC (3.1) would say that
>warning: passing arg 1 of 'func' from incompatible pointer type


But still my code in 'func' (not posted, above is an example) works
correctly, utilizing whole '**table'.

Now what I want to ask is that which is the legal way to make a call of
this kind? Or is it just a bug or "feature" of GCC?
I am compiling with a commandline:

gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2


Your code exactly as written here, pasted to tatu.c and compiled with ..

gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2 tatu.c

... compiles here without error.

C:\work\c\clc>g cc --version
gcc.exe (GCC) 3.1
Copyright (C) 2002 Free Software Foundation, Inc.

--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4
Ok. First post was wrong. (I thought that simplifying would do the same. It
didn't.)

Here we are:
42:int fprint_comments _formatted
43: ( FILE *tg
44: , const vorbis_comment *vc)
45:{
46: register int i;

....

69: if (flag) {

70: entry = has_str (s_album, vc->user_comment s, vc->comments);

71: val = strpbrk (vc->user_comment s[entry], "=");
72: val++;
73: fprintf (tg, "%s\n", val);
74:
75: flag = 0;
76: }

....

90: return 0;
91:}

gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2 cdmaker.c cdmaker_1.o -o
cdmaker.exe
cdmaker.c In function 'fprint_comment s_formatted':
cdmaker.c:70 warning: passing arg 2 of 'has_str' from incompatible pointer type
Function prototypes:
int fprint_comments _formatted
( FILE *tg
, const vorbis_comment *vc);

int has_str
( const char *str
, const char **table
, int ent); /* Number of entries in '**table' */
Struct definition:
typedef struct {
char **user_comments ;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
Nov 14 '05 #5
On Sun, 12 Dec 2004 03:43:16 GMT, Tatu Portin <ax****@mbnet.f i> wrote:
Ok. First post was wrong. (I thought that simplifying would do the same. It
didn't.)

Here we are:
42:int fprint_comments _formatted
43: ( FILE *tg
44: , const vorbis_comment *vc)
45:{
46: register int i;

...

69: if (flag) {

70: entry = has_str (s_album, vc->user_comment s, vc->comments);


You might get a bit more help compiling it with a C++ compiler:

E:\temp>gpp -Wall a.cpp
a.cpp: In function `int main()':
a.cpp:27: error: invalid conversion from `char**' to `const char**'
a.cpp:27: error: initializing argument 2 of `int has_str(const char*,
const char**, int)'

It's valid in C, but GCC decides it's worth raising a warning about
converting 'char**' to 'const char**'. You can get rid of the warning by
doing a type cast, but there may be better methods of dealing with it.
Nov 14 '05 #6
In article <o7***********@ read3.inet.fi> Tatu Portin <ax****@mbnet.f i> wrote:
Ok. First post was wrong. (I thought that simplifying would do the same. It
didn't.)


Always test the simplified-for-post version. :-)

Without quoting a lot, your problem is the "const" qualifier,
which does not work right in C. Stop using it, and the problem
will go away (this is not necessarily the best solution, but is
certainly the easiest to describe).

See also the comp.lang.c FAQ, question 11.10.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #7
Mike Wahler wrote:

"Tatu Portin" <ax****@mbnet.f i> wrote in message
news:TE******** *******@read3.i net.fi...
I have a struct:

typedef struct {
char **user_comments ;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
void func (char **table)
{
return;
}

int main (void)
{
vorbis_comment vc;
/* This part GCC doesn't like */

func (vc.user_commen ts);

return 0;
}

GCC (3.1) would say that
warning: passing arg 1 of 'func' from incompatible pointer type


But still my code in 'func' (not posted,


Why not?
above is an example) works correctly,
utilizing whole '**table'.

Now what I want to ask is that which is the legal way to make a call of

this
kind? Or is it just a bug or "feature" of GCC?


We cannot tell you the correct form for calling a function whose
definition (at least declaration) that we cannot see. What is
the signature of 'func()'? Remember that an array is not a pointer,
and a pointer is not an array.


Er - he gave the full definition of func. What he didn't do was
initialize vc, which should not cause a compile error.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #8
"CBFalconer " <cb********@yah oo.com> wrote in message
news:41******** *******@yahoo.c om...
Mike Wahler wrote:

We cannot tell you the correct form for calling a function whose
definition (at least declaration) that we cannot see. What is
the signature of 'func()'? Remember that an array is not a pointer,
and a pointer is not an array.


Er - he gave the full definition of func. What he didn't do was
initialize vc, which should not cause a compile error.


Um, yes, it seems that it's me who can't see today. :-)

-Mike
Nov 14 '05 #9
In article <8e************ *************** *****@4ax.com>
Raymond Martineau <bk***@ncf.ca > wrote:
You might get a bit more help compiling it with a C++ compiler:

E:\temp>gpp -Wall a.cpp
a.cpp: In function `int main()':
a.cpp:27: error: invalid conversion from `char**' to `const char**'
a.cpp:27: error: initializing argument 2 of `int has_str(const char*,
const char**, int)'

It's valid in C ...
Actually, it is *not* valid in C either.
but GCC decides it's worth raising a warning about converting
'char**' to 'const char**'.
GCC simply chooses to complain-and-keep-going in this case, rather
than complain-and-stop-compiling. GCC calls the former a "warning"
and the latter an "error", but the C standard says only that the
conversion is incorrect and must elicit a "diagnostic ".

The set of things-that-cause-stopping is different for GCC's C
compiler than for GCC's C++ compiler, partly because they are
maintained by different people, partly because C compilers have
traditionally accepted all kinds of invalid source and generated
machine code anyway (which promptly core-dumps, in many cases),
and of course partly because the languages are different (although
in this case, the semantics actually match up, for once).
You can get rid of the warning by doing a type cast, but there may
be better methods of dealing with it.


In C++, if you made has_str() take a "const char *const *" parameter,
the problem would go away, but it would remain a problem in C.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10182
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
8
4117
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
8
29069
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If possible how could one pass these seven values in the array to a function that would check the values. I tried return y but it didn't work
11
4471
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
11
8131
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
2
2196
by: sonaliagr | last post by:
I am trying to update a msg array in function by passing the address but it is showing an error. and also, i want the value of msg array to be accessible to the full code that is inside the main function...I hope i am making sense...Please look at the code and help me in pointing out the error.. #include<stdio.h> #include<stdlib.h> #include<time.h>
3
3495
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/ #include <cstdio> #define MAX_WORD_LEN 80 #define MAX_SIZE 1000
5
2410
by: mshaaban | last post by:
Hello, In my code I have a large static 2D arrays defined as: code: #define LONMAX 1440 #define LATMAX60 480 void main (int argc, char *argv)
20
2186
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then suddenly I can't use sizeof(array) / sizeof(array) anymore within that function ? Help - What point am I missing ?
8
3506
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7530
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5422
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.