1753769 Members
5277 Online
108799 Solutions
New Discussion юеВ

Regarding char pointer

 
vind123
Regular Advisor

Regarding char pointer

My C program is running under HP-UX 10.2

char *rc = NULL

short *dt;
*dt=1;

switch (*dt)
{
case 1:
rc="Intertoll"
break;
case 2:
rc="LocalExchange"
break;
case 3:
rc="other"
break;
}

Printf("%s",rc);

The output is display as "other" even when dt is 1. It should have displayed as intertoll I had pasted an sample piece of code from my application. I don't know why char pointer rc is showing an incorrect value event if dt is correct.
Dont know is there a problem with char pointer?
8 REPLIES 8
Steven Schweda
Honored Contributor

Re: Regarding char pointer

!. That's not a C program.

2. If it were, then "dt" would be a pointer
to nowhere. Declaring a pointer does not
create the storage to which it points.

3. If the pointer "dt" actually pointed to
some storage, then it might have worked
as you expected. That is, for example,
insert:

short dt_real; /* Allocate a short. */
dt = &dt_real; /* Point "dt" to it. */

after "short *dt;".
Sandman!
Honored Contributor

Re: Regarding char pointer

You cannot initialize a pointer to the value of a variable. It can be initialized (or made to point to an object) by getting the address of the object. Once the pointer variable points to an object then you can dereference it i.e.

short x, *ip
x = 1;
*ip = 1; /* wrong use of pointer */
ip = &x; /* correct */
*ip = 10; /* x in now equal to 10 since *ip is the same as x */
vind123
Regular Advisor

Re: Regarding char pointer

Apologies i posted the incorrect info about dt. dt is fine. It happens to be correct only.
char *rc = NULL
short val;
val=1;
short *dt;
dt=&val;
switch (*dt)
{
case 1:
rc="Intertoll"
break;
case 2:
rc="LocalExchange"
break;
case 3:
rc="other"
break;
}

Printf("%s",rc);

rc is not getting stored as "intertoll", it's getting stored as "other". I did a printf statement on the rc in the switch case after rc="intertoll" and it shows an incorrect value. It's going to case 1 only. Just wondering whether char pointer has some problems if we assign directly. I will try to get a snapshot from the real code after some time. I had pasted the how it appears in the real above

Sandman!
Honored Contributor

Re: Regarding char pointer

>Apologies i posted the incorrect info about dt. dt is fine. It happens to be correct

Are you sure you'ave posted the correct script this time? Most of your statements are missing the terminating semi-colon and your Printf() starts with an uppercase "P"?

Post the complete code so that it's easier to troubleshoot.

~thanks
vind123
Regular Advisor

Re: Regarding char pointer

100 static char*
101 worker( swclli, a_end_clli, z_end_clli, tfuse, ttm, is_local,
102 topas_tfuse, dt_tfuse)
103 char *swclli;
104 char *a_end_clli;
105 char *z_end_clli;
106 char *tfuse;
107 char *ttm;
108 short *is_local;
109 short *topas_tfuse;
110 short *dt_tfuse;
111 {
112 int err = 0;
113 int warn = 0;
114 char *tollClli = "?";
115 char *rc = NULL;
..................



195 if (! err) switch ( *dt_tfuse ) {
196 case DT_OWN_INTERTOLL:
197 rc = "intertoll";
198 break;
199
200 case DT_OWN_LOCEX:
201 rc = "localexchange";
202 break;
203
204 case DT_OWN_NODAL:
205 rc = "nodal";
206 break;
207
208 case DT_OWN_INTL:
209 rc = "international";
210 break;
211
212 case DT_OWN_ADJUNCT:
213 rc = "adjunct";
214 break;
215
216 case DT_OWN_LOCALUSE:
217 rc = "localuse";
218 break;
219
220 case DT_OWN_OTHER:
221 rc = "other";
222 break;
223
224 default:
225 err = __LINE__;
226 break;
227 }


......


241 return rc

}


By Putting trace message i am able to observe *dt_tfuse value is DT_OWN_INTL before coming to switch..case statement. Also i had put a trace message, to display the rc value after linenumber
209 rc = "international";
The trace message shows after the execution of line number 209, rc as "other". Pls post me whether i need to paste the entire function.
The function has 240 lines so i had pasted a part of it.
Steven Schweda
Honored Contributor

Re: Regarding char pointer

You might post something which is possible to
compile and run. Without line numbers. You
know, an actual program?

Even better, include the commands you used to
compile/link your test program, and an actual
transcript showing how you ran it, and what
happened when you ran it.
vind123
Regular Advisor

Re: Regarding char pointer

The compilation i will be doing as nmake Makefile. It has a lot of files along with which i will compile. I am trying to find whether i can detect something thru gdb debugger.

1.I have niOOSdb.ec(embedded C file for query the database) in my project in which there is a function nigetOOSrptDB. I am unable to keep a breakpoint in this file. If i do a step on the function call nigetOOSrptDB in another c file it's completing the function and getting out. Is it possible to keep a break point in .ec file? Is gdb debugger allows only to keep a breakpoint in c/c++ file.

2. I am trying to display the local variable inside a function inside gdb But i dont know how to display the value of buffer in gdb. It's showing addresses if i do a (gdb) print path_n_file
$4 = {d = 0x40217658}


(gdb) bt
#0 OOSsender::process_sw_clli_det (this=0x402076d0, sw_name=@0x7b03aee8, row_acc=@0x7b03addc) at ../src/niOOSrpt.C:258
#1 0x15390 in OOSmainRpt (argc=1, input_sw=@0x7b03ad0c) at ../src/OOSrptMain.C:204
#2 0x13ee0 in main (argc=1, argv=0x7b03aac0) at ../src/OOSrptMain.C:100
(gdb) frame 0
#0 OOSsender::process_sw_clli_det (this=0x402076d0, sw_name=@0x7b03aee8, row_acc=@0x7b03addc) at ../src/niOOSrpt.C:258
258 int ret = nigetOOSrptDB ( sw_name, (long)(atoi(str_hrs)),
(gdb) info locals
ret = 0
tmp_path_n_file_sum = {d = 0x402174c8}
__R00e7 = {d = 0x40217518}
__R00e8 = {d = 0x402174c8, strung = 1 '\001'}
path_n_file = {d = 0x40217658}
__R00e2 = {d = 0x40207b28}
__R00e3 = {d = 0x40217658, strung = 1 '\001'}
__R00e4 = {d = 0x40217608}
tmp_path_n_file = {d = 0x40217568}
__R00e5 = {d = 0x402175b8}
__R00e6 = {d = 0x40217568, strung = 1 '\001'}
buffer = {d = 0x402048d8}
str_outage = {d = 0x402079e8}
str_hrs = {d = 0x40207648}
str_row = {d = 0x402048d8}
str_gaps = {d = 0x4020a060}
hrs_li = {> = { = {theLizt = 0x40207714, nextIt = 0x0, cache = 0x0,
cacheNo = 0, index = 1, pred = 0x4020a6a8}, }, }
row_li = {> = { = {theLizt = 0x40207720, nextIt = 0x0, cache = 0x0,
cacheNo = 0, index = 0, pred = 0x0}, }, }
gap_li = {> = { = {theLizt = 0x4020772c, nextIt = 0x0, cache = 0x0,
cacheNo = 0, index = 1, pred = 0x4020a6f8}, }, }
out_li = {> = { = {theLizt = 0x40207738, nextIt = 0x0, cache = 0x0,
cacheNo = 0, index = 1, pred = 0x4020a718}, }, }
fp_output = (struct FILE *) 0x0
_result = 62035




int
212 OOSsender::process_sw_clli_det( const String& sw_name, long &row_acc )
213 {
214 // generate the detail trunk OOS report and push it
215 // called from from OOSmain() with one switch clli at a time.
216
217 FILE *fp_output=NULL;
218 row_acc=0; // total rows accumulated so far
219
220 DT_IN_FUNC1("OOSsender::process_sw_clli_det")
221
222 if ( dt_debug ) {
223 fprintf(stderr,"process_sw_clli_det sw_name =<%s>\n", (const char*)sw_name);
224 }
225
226 // get data from database loaded into tmp_path_n_file
227 String path_n_file = this->GETdirname() + "o" + generateJulianFileName();
228 String tmp_path_n_file = "/tmp/O" + generateJulianFileName();
229
230 String buffer;
231 String str_outage;
232 String str_hrs;
233 String str_row;
234 String str_gaps;
235 Listiter hrs_li(this->det_hrs_list);
236 Listiter row_li(this->row_tot_list);
237 Listiter gap_li(this->det_gap_list);
238 Listiter out_li(this->det_outage_list);
239 hrs_li.reset ();
240 row_li.reset ();


3. Also if there is a variable char *ptr. How do i print the value at the address pointed by ptr and address contained in the ptr



Sandman!
Honored Contributor

Re: Regarding char pointer

The code below will output the desired value of rc and shows howto...
>print the value at the address pointed by ptr and address contained in the ptr<

~hope it helps

==============================================================
#include

int main(void)
{
char *rc;
short dt = 1, *pdt = &dt;

switch (*pdt)
{
case 1:
rc="Intertoll";
break;
case 2:
rc="LocalExchange";
break;
case 3:
rc="other";
break;
}
printf("value at address 0x%lx is %lu\n", pdt, *pdt);
printf("%s\n", rc);
}