?
Solved

Splitting a pointer

Posted on 2000-02-27
4
Medium Priority
?
247 Views
Last Modified: 2010-04-15
What am I doing wrong here?  I am trying to split this pointer into two vars,

Can anyone help?


char *tdom, *tuser;
if ((tdom = strchr(p->pop_parm[1], 47)))
{
*tdom = '\0';
memcpy(tuser, p->pop_parm[1], sizeof(p->pop_parm[1]));
tdom++;
}
tuser[strlen(tuser) - strlen(tdom) - 1] = '\0';
(void)strncpy(p->domain, tdom, sizeof(p->domain) - 1);
p->domain[sizeof(p->domain)-1] = '\0';
(void)strncpy(p->user, tuser, sizeof(p->user));
p->user[sizeof(p->user)-1] = '\0';
pop_msg(p, POP_SUCCESS, "User: %s %d %d, Domain: %s %d %d",  p->user, sizeof(p->pop_parm[1]), sizeof(p->user), p->domain, sizeof(tdom), sizeof(p->domain));

0
Comment
Question by:Nitro187
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
AndrewRodionov earned 300 total points
ID: 2563819
Hello Nitro187!

I think your problem consists in using pointer that is NOT initialized. Look,
you declare tuser and it points to... nothing! It looks like an auto variable therefore it contains garbage until you assign a reasonable value. So you'll get an access violation in

memcpy(tuser, p->pop_parm[1], sizeof(p->pop_parm[1]));

Besides, tdom = strchr(p->pop_parm[1], 47) can return NULL and your assignment *tdom = '\0'; can fail too...

There are some incomprehensibilities and I need more information about p pointer.

Andrew


0
 

Author Comment

by:Nitro187
ID: 2566881
Hey,  ok, here is the struct, which P comes from:

typedef struct  {
int                 debug;
int                 xmitting;
int                 stats;
int                 dirty;
int                 kerberos;
int                 server_mode;
char            *   myname;
char            *   myhost;
char            *   client;
char            *   ipaddr;
unsigned short      ipport;
char                user[MAXUSERNAMELEN];
char                domain[MAXDOMAINLEN];
state               CurrentState;
int                 msgs_deleted;
int                 last_msg;
long                bytes_deleted;
char                drop_name[MAXDROPLEN];
char                temp_drop[MAXDROPLEN]
long                drop_size;
long                spool_end;
FILE            *   drop;              
FILE            *   input;
FILE            *   output;
FILE            *   trace;
FILE            *   hold;
char            *   pop_parm[MAXPARMCOUNT];
int                 parm_count;
char            *   bulldir;
} POP;

Any more ideas?
0
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2567608
Other code (from (void)strncpy(p->domain,... ) looks like correct. Once more, your problem is in tuser pointer.

What does it point to? I think it's NOT initialized!..

Andrew
0
 

Author Comment

by:Nitro187
ID: 2571297
Ahh,  I had a tuser, and a ttuser,  I used the wrong one to init.  Thanks.. :P
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question