GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Apr-2004, 11:00
enomad enomad is offline
New Member
 
Join Date: Apr 2004
Posts: 2
enomad is on a distinguished road

Passing VARCHAR to function


Server: HP-UX B.11.11

I am getting different results when I print the VARCHAR array in the main function and in the called function.

code:
CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <sqlca.h>

#define MAX_RULES       5

int fnDummy(VARCHAR *);

void main(int argc, char *argv[])
{
        int i=0;
        static VARCHAR  lv_logic_ops[MAX_RULES][4];

        strcpy(lv_logic_ops[0].arr,"AND");
        lv_logic_ops[0].len = strlen(lv_logic_ops[0].arr);
        lv_logic_ops[0].arr[lv_logic_ops[0].len] = '\0';
        strcpy(lv_logic_ops[1].arr,"AND");
        lv_logic_ops[1].len = strlen(lv_logic_ops[1].arr);
        lv_logic_ops[1].arr[lv_logic_ops[1].len] = '\0';
        strcpy(lv_logic_ops[2].arr,"OR");
        lv_logic_ops[2].len = strlen(lv_logic_ops[2].arr);
        lv_logic_ops[2].arr[lv_logic_ops[2].len] = '\0';
        strcpy(lv_logic_ops[3].arr,"AND");
        lv_logic_ops[3].len = strlen(lv_logic_ops[3].arr);
        lv_logic_ops[3].arr[lv_logic_ops[3].len] = '\0';
        strcpy(lv_logic_ops[4].arr,"OR");
        lv_logic_ops[4].len = strlen(lv_logic_ops[4].arr);
        lv_logic_ops[4].arr[lv_logic_ops[4].len] = '\0';

        for(i=0;i<5;i++)
                fprintf(stderr,"---- [%s] [%d] -----\n",lv_logic_ops[i].arr,lv_logic_ops[i].len);

        i = fnDummy(lv_logic_ops);

}

int fnDummy(VARCHAR *lv_logic_ops)
{
        int i=0;

        for (i=0;i < 10;i++)
                fprintf(stderr,"*** [%s] ***\n",lv_logic_ops[i].arr);
}

output:
Quote:
---- [AND] [3] -----
---- [AND] [3] -----
---- [OR] [2] -----
---- [AND] [3] -----
---- [OR] [2] -----
*** [AND] ***
*** [] ***
*** [AND] ***
*** [] ***
*** [OR] ***
*** [] ***
*** [AND] ***
*** [] ***
*** [OR] ***
*** [] ***



I would really appreciate if someone can tell me why I am getting different outputs.
Thanx!
Last edited by dsmith : 29-Apr-2004 at 11:07. Reason: Use [c] & [/c] for syntax highlighting.
  #2  
Old 29-Apr-2004, 11:23
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi enomad. I moved your post to the C forum.

I am having a bit of trouble following what you are doing. Do you have a definition for the structure VARCHAR? Does it only include an arr and len entry and how are these defined?

The base of the problem (I think) has to do with the fact that you define a 2d array of type VARCHAR here:
CPP / C++ / C Code:
static VARCHAR  lv_logic_ops[MAX_RULES][4];

Then all of your assignments use single indexing:
CPP / C++ / C Code:
strcpy(lv_logic_ops[0].arr,"AND");
lv_logic_ops[0].len = strlen(lv_logic_ops[0].arr);
lv_logic_ops[0].arr[lv_logic_ops[0].len] = '\0';

This works just fine, but I am wondering why you are defining this as a 2d array?

Finally, when you give the header definition for fnDummy, it is going to treat whatever you send it as a linear 1d array. Somewhere you need to tell it that this is a 2d array:
CPP / C++ / C Code:
int fnDummy(VARCHAR *lv_logic_ops)

I think this should be more like to let it know what you are passing:
CPP / C++ / C Code:
inf fnDummy(VARCHAR lv_logic_ops[][4])

This is just speculation, since I don't know what VARCHAR is. It also won't compile under linux (I don't have sqlca.h where I believe VARCHAR is defined). I tried to emulate it, but I get no luck.

Maybe if you could explain your declerations a bit more I could try to help more?

Sorry,
d
  #3  
Old 29-Apr-2004, 13:38
enomad enomad is offline
New Member
 
Join Date: Apr 2004
Posts: 2
enomad is on a distinguished road
Snippet from Ora Doc:
-------------------------------
Think of a VARCHAR as an extended C type or pre-declared struct. For example, the precompiler expands the VARCHAR declaration

VARCHAR username[20];

into the following struct with array and length members:

struct
{
unsigned short len;
unsigned char arr[20];
} username;
--------------------------------

I changed the code as suggested and changed VARCHAR to char, and it is working fine. But for VARCHAR its printing it differently. I am not able to figure out what I am doing wrong.

changed code:
Quote:
#include <stdio.h>
#include <string.h>
#include <sqlca.h>

#define MAX_RULES 5

int fnDummy(char ops[][4]);

void main(int argc, char *argv[])
{
int i=0;
static char lv_logic_ops[MAX_RULES][4];

strcpy(lv_logic_ops[0],"AND");
lv_logic_ops[0][strlen(lv_logic_ops[0])] = '\0';
strcpy(lv_logic_ops[1],"AND");
lv_logic_ops[1][strlen(lv_logic_ops[0])] = '\0';
strcpy(lv_logic_ops[2],"OR");
lv_logic_ops[2][strlen(lv_logic_ops[0])] = '\0';
strcpy(lv_logic_ops[3],"AND");
lv_logic_ops[3][strlen(lv_logic_ops[0])] = '\0';
strcpy(lv_logic_ops[4],"OR");
lv_logic_ops[4][strlen(lv_logic_ops[0])] = '\0';

for(i=0;i<5;i++)
fprintf(stderr,"---- [%s] [%d] -----\n",lv_logic_ops[i],strlen(lv_logic_ops[i]));

i = fnDummy(lv_logic_ops);

}

int fnDummy(char lv_logic_ops[][4])
{
int i=0;

for (i=0;i < 5;i++)
fprintf(stderr,"*** [%s] ***\n",lv_logic_ops[i]);
}


Thanks for the help d!
  #4  
Old 29-Apr-2004, 14:03
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by enomad
Snippet from Ora Doc:
-------------------------------
Think of a VARCHAR as an extended C type or pre-declared struct. For example, the precompiler expands the VARCHAR declaration

VARCHAR username[20];

into the following struct with array and length members:

struct
{
unsigned short len;
unsigned char arr[20];
} username;
--------------------------------


Yeah, that makes more sense. Sorry, my compiler does not support that and it is new to me. Why don't you just define your own structure and then use it as a 1D array?

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <sqlca.h>

#define MAX_RULES       5

struct{ 
    unsigned short  len; 
    unsigned char   arr[4]; 
}lv_logic_ops[MAX_RULES]; 

int fnDummy(lv_logic_ops*);

void main(int argc, char *argv[])
{
        int i=0;

        strcpy(lv_logic_ops[0].arr,"AND");
        lv_logic_ops[0].len = strlen(lv_logic_ops[0].arr);
        lv_logic_ops[0].arr[lv_logic_ops[0].len] = '\0';
        strcpy(lv_logic_ops[1].arr,"AND");
        lv_logic_ops[1].len = strlen(lv_logic_ops[1].arr);
        lv_logic_ops[1].arr[lv_logic_ops[1].len] = '\0';
        strcpy(lv_logic_ops[2].arr,"OR");
        lv_logic_ops[2].len = strlen(lv_logic_ops[2].arr);
        lv_logic_ops[2].arr[lv_logic_ops[2].len] = '\0';
        strcpy(lv_logic_ops[3].arr,"AND");
        lv_logic_ops[3].len = strlen(lv_logic_ops[3].arr);
        lv_logic_ops[3].arr[lv_logic_ops[3].len] = '\0';
        strcpy(lv_logic_ops[4].arr,"OR");
        lv_logic_ops[4].len = strlen(lv_logic_ops[4].arr);
        lv_logic_ops[4].arr[lv_logic_ops[4].len] = '\0';

        for(i=0;i<5;i++)
                fprintf(stderr,"---- [%s] [%d] -----\n",lv_logic_ops[i].arr,lv_logic_ops[i].len);

        i = fnDummy(lv_logic_ops);

}

int fnDummy(VARCHAR *lv_logic_ops)
{
        int i=0;

        for (i=0;i <5;i++)
                fprintf(stderr,"*** [%s] ***\n",lv_logic_ops[i].arr);
}

I know I am probably missing something, but since VARCHAR is a preprocessor directive anyway, this is what the compiler would see anyway isn't it?

BTW - if you enclose your c-code between [c] and [/c] tags you can get C-syntax highlighting which is much easier on the eyes.

Also, maybe you could explain why you are null-terminating the strings? Does your compiler not null-terminate string literals? Just curious...

Cheers,
d
  #5  
Old 29-Apr-2004, 14:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I'm not following a portion of the logic you're using.
I'm making the code here a little simpler just so it can be followed easier, changing:
CPP / C++ / C Code:
strcpy(lv_logic_ops[0],"AND");
lv_logic_ops[0][strlen(lv_logic_ops[0])] = '\0';
to
CPP / C++ / C Code:
strcpy(sbuff,"AND");
sbuff[strlen(sbuff)] = '\0';
First line copys the values 0x41, 0x4E1, 0x44, 0x00 into sbuff
strlen[sbuff] returns the value 3 because it stops at the 0x00
sbuff[xxx] = '\0'; replaces the 0x00 with 0x00. There seems to be no reason for the entire second line in this code.

remember, the strxxx functions all work based off a trailing '\0' (0x00) in any string.
__________________

Age is unimportant -- except in cheese
  #6  
Old 29-Apr-2004, 14:32
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by WaltP
remember, the strxxx functions all work based off a trailing '\0' (0x00) in any string.

That is absolutely correct. I was thrown off by this as well thinking that for some reason the compiler wasn't null-terminating the literals, but if that were the case, the str functions would just keep going until they arbitrarily hit a null.
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59
need help with passing 3 arrays into a function tommy69 C Programming Language 14 07-Apr-2004 00:22
Strange behaviour in printf (interesting) nusstu C Programming Language 9 05-Apr-2004 13:36
Another problem dealing with main() and driver function tommy69 C Programming Language 4 20-Mar-2004 19:46
Calling functions within a function spudtheimpaler C Programming Language 5 02-Mar-2004 08:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.