0

I am using code generated with gSOAP, and need to use declarations as they have been provided if possible.

gSOAP's generated code provides the following prototype:

soap_call___accounts(struct soap *soap, struct _acnt *acnt, struct _resp *resp);

as well as the following struct definition:

struct _acnt
{
       int sizeacnt;
       char **acntNum;
};

In my calling application I need to send an account number such as "00000123" using the structure member acntNum as part of the acnt argument in the calling function, but before it can be used to do that, it needs to be initialized.

How is char **acntNum initialized?

ryyker
  • 22,849
  • 3
  • 43
  • 87

2 Answers2

1
char *acntNumP = malloc(strlen("00000123")+1);  
strcpy(acntNumP, "00000123");  
char ** acntNum = &acntNumP;

Check for NULLs where needed, of course.

N 1.1
  • 12,418
  • 6
  • 43
  • 61
littleadv
  • 20,100
  • 2
  • 36
  • 50
  • 4
    Don't cast the return value from `malloc()`. Doing that can hide errors in your code (namely failure to include `` with the consequent misinterpretation of the return value). – pmg Mar 22 '11 at 00:04
  • @pmg: how does casting `void*` to `char*` hide missing include? – littleadv Mar 22 '11 at 00:10
  • 5
    There is no `void*` when you don't include the proper header. In the absence of a declaration the compiler assumes functions return `int`. Casting `int` to `char*` is the issue generated by not including the header. – pmg Mar 22 '11 at 00:18
  • @pmg: OK, removed the casting from the answer. Frankly, I never thought of writing bad code as a way to catch errors. I would expect a compiler warning because of the implicit casting, and I don't take code with warnings or errors as an example of how things should be done. – littleadv Mar 22 '11 at 00:43
  • @littleadv: This is one of many inheritances from the early days of c. The original K&R language assumed that all functions returned `int` unless it was told otherwise, and subsequent versions have only slowing ruled this kind of thing out (to avoid breaking existing code). – dmckee --- ex-moderator kitten Mar 22 '11 at 01:40
  • @littleadv: casts in C are almost always wrong. Rely on implicit conversions as much as you can and let the compiler do the work (and warn you if it can't). – pmg Mar 22 '11 at 09:03
  • @littleadv: omitting the cast is not bad code, since a `void*` can be converted to any pointer type implicitly. A compiler should never issue a warning for `char *p = malloc(n);`. – Fred Foo Mar 26 '11 at 12:49
1

See also the gSOAP 2.8.1 User Guide for more information.

Your soap function accepts an array of accounts. So you can not only call the soap function for the account "00000123", but also the two accounts ["00000123","00000456"] is possible in a single call.

To make this work, you must not only allocate memory, but also set the size parameter to the number of accounts you pass. For example you can do this:

struct acnt Accounts;
char *AccountToCheck = "00000123";

Accounts.sizeacnt=1;
Accounts.acntNum = malloc(1 * sizeof(*Accounts.acntNum));
Accounts.acntNum[0] = AccountToCheck;

soap_call___accounts(soap, &Accounts, &Response);
wimh
  • 15,072
  • 6
  • 47
  • 98
  • 2
    Same issue as with littleadv's answer: don't cast the result from `malloc`. – Fred Foo Mar 26 '11 at 12:50
  • Ok changed it after reading http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc – wimh Mar 26 '11 at 13:06