dreference argv

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 12
Reputation: krellor is an unknown quantity at this point 
Solved Threads: 0
krellor krellor is offline Offline
Newbie Poster

dreference argv

 
0
  #1
Apr 14th, 2009
Stupid question. Haven't done production C in a while and just inherited some legacy code. In case it makes a difference it is running out of OMVS on a Z/OS IBM mainframe. The C program is invoked by an assembler program. The assembler program passes in a number arguments that are simply addresses. Some are addresses that contain addresses, and some are just addresses.

The C program then uses strcpy and memcpy to move data back and forth between the assembler program and the C program. I'm at the point where I'm trying to save some space. Currently I am copying the passed addresses to a pointer variable, and then using that variable in strcpy. For example

  1. char *cTempPtr = NULL, search[256] = "init";
  2. cTempPtr = argv[1];
  3. strcpy(search, cTempPtr);

What I would like to do is cut out the need for the extra pointer variable and just dereference the address contained in argv[1]. I have tried numerous combinations of *argv[1], etc... but apparently I just don't remember by reference/dereference logic well enough. I also haven't found any useful hits on google. To clarify I would like the code to look like:

  1. char *cTempPtr = NULL, search[256] = "init";
  2. strcpy(search, *argv[1]);

That is the basic idea anyway. Basically, the value of argv[1] is an address that points to the first byte of a null terminated character array that has been allocated in the assembler program. So, I want strcpy to use the address in argv[1] to copy that value, without having to use a middleman in the form of a pointer variable. But all my attempts to dereference argv[1] have failed. Any help will be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,678
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: dreference argv

 
1
  #2
Apr 14th, 2009
This is the original code, right?
  1. char *cTempPtr = NULL, search[256] = "init";
  2. cTempPtr = argv[1];
  3. strcpy(search, cTempPtr);
The equivalent of that without using cTempPtr is as follows:
  1. char search[256] = "init";
  2. strcpy(search, argv[1]);
There's no magic involved between assigning cTempPtr and using it in strcpy, the address is the same. If you use *argv[1] instead, you've got a type mismatch because *argv[1] is equivalent to argv[1][0], which is the first character of the second string in argv. The second argument of strcpy expects a pointer to char, not a char.
Last edited by Narue; Apr 14th, 2009 at 4:27 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 12
Reputation: krellor is an unknown quantity at this point 
Solved Threads: 0
krellor krellor is offline Offline
Newbie Poster

Re: dreference argv

 
0
  #3
Apr 14th, 2009
Ok, that makes sense. But to clarify, if argv[1] holds the value "1234" will using strcpy that way that I am above cause it to copy "1234" or will it go to the address space "1234" and start copying there?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,678
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: dreference argv

 
1
  #4
Apr 14th, 2009
strcpy doesn't know or care if the string represents an address. If you want to copy the contents of the address represented by the string, you first have to convert the address to a suitable type. For example:
  1. int addr = (int)strtol ( argv[1], NULL, 0 );
  2. strcpy ( search, (char*)addr );
Exactly how you go about the conversion depends on how the address is stored in argv. It could be the string representation of an address (eg. "0x12345"), or it could be a pun of the data where each character matches a byte of the address. The above code assumes the former.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 14
Reputation: DavidOsorno is an unknown quantity at this point 
Solved Threads: 1
DavidOsorno DavidOsorno is offline Offline
Newbie Poster

Re: dreference argv

 
0
  #5
Apr 14th, 2009
It is not clear to me why you try to deference argv[1]

If what you want is to take a portion of argv[1] content, you can do as follows:

strncpy(pPointer,pSource,nNumberOfBytes);




Originally Posted by krellor View Post
Stupid question. Haven't done production C in a while and just inherited some legacy code. In case it makes a difference it is running out of OMVS on a Z/OS IBM mainframe. The C program is invoked by an assembler program. The assembler program passes in a number arguments that are simply addresses. Some are addresses that contain addresses, and some are just addresses.

The C program then uses strcpy and memcpy to move data back and forth between the assembler program and the C program. I'm at the point where I'm trying to save some space. Currently I am copying the passed addresses to a pointer variable, and then using that variable in strcpy. For example

  1. char *cTempPtr = NULL, search[256] = "init";
  2. cTempPtr = argv[1];
  3. strcpy(search, cTempPtr);

What I would like to do is cut out the need for the extra pointer variable and just dereference the address contained in argv[1]. I have tried numerous combinations of *argv[1], etc... but apparently I just don't remember by reference/dereference logic well enough. I also haven't found any useful hits on google. To clarify I would like the code to look like:

  1. char *cTempPtr = NULL, search[256] = "init";
  2. strcpy(search, *argv[1]);

That is the basic idea anyway. Basically, the value of argv[1] is an address that points to the first byte of a null terminated character array that has been allocated in the assembler program. So, I want strcpy to use the address in argv[1] to copy that value, without having to use a middleman in the form of a pointer variable. But all my attempts to dereference argv[1] have failed. Any help will be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,607
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: dreference argv

 
-2
  #6
Apr 14th, 2009
Originally Posted by DavidOsorno View Post
It is not clear to me why you
shush. grownups are talking, now.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 12
Reputation: krellor is an unknown quantity at this point 
Solved Threads: 0
krellor krellor is offline Offline
Newbie Poster

Re: dreference argv

 
0
  #7
Apr 14th, 2009
Ok. Well, let me post a section of the original code and then my re-write of it.

Original Code
  1. int main (int argc, char *argv[])
  2. { /* 01 */
  3. FILE *fd;
  4. char host[20]="xups.uni.edu";
  5. char req[005]="init";
  6. char key[050]="init";
  7. char msg[256]="init";
  8. char nid[256]="Not Found";
  9. char uni_num[256]="Not Found";
  10. char email[256]="Not Found";
  11. char *pt1, *pt2, *pt3, *pt4, *pt5, *pt6;
  12. int *ptr_nid, *ptr_unino, *ptr_email, *ptr_msg, *ptr;
  13. int nid_adr, unino_adr, email_adr, msg_adr;
  14.  
  15. pt1 = &req[0];
  16. pt2 = &key[0];
  17. pt3 = argv[0];
  18. pt4 = argv[1];
  19. if (argc == 3) {
  20. pt3 = argv[1];
  21. pt4 = argv[2];
  22. }
  23. if (argc == 7) {
  24. pt5 = argv[6];
  25. pt6 = &host[0];
  26. strcpy (pt6,pt5);
  27. }
  28.  
  29. strcpy ( pt1,pt3);
  30. strcpy ( pt2,pt4);
  31.  
  32. query_ups(req, key, uni_num, nid, email, msg, host);


Rewrite
  1. main(int argc, char **argv)
  2. {
  3. char nid.256.=" ",
  4. uniID[256] = "",
  5. email[256]="",
  6. employeeID[256] = "",
  7. searchField[256] = "",
  8. searchValue[256] = "",
  9. *cTempPtr = NULL;
  10. int *msgPtr, *nidPtr, *uninoPtr, *emailPtr, *tempPtr;
  11. int doMemOut = 0, echo = 1;
  12.  
  13. switch(argc)
  14. {
  15. case 3:
  16. if(strcmp(argv[1], "alias") == 0)
  17. {
  18. strncpy(searchValue,argv[2], sizeof(nid) - 1);
  19. strcpy(searchField, "CN");
  20. }
  21. else if(strcmp(argv[1], "unino") == 0)
  22. {
  23. strncpy(searchValue, argv[2], sizeof(uniID) - 1);
  24. strcpy(searchField, "employeeID");
  25. }
  26. else if(strcmp(argv[1], "email") == 0)
  27. {
  28. strncpy(searchValue, argv[2], sizeof(email) - 1);
  29. strcpy(searchField, "uniEduEmailForwardingAddress");
  30. }
  31. else
  32. {
  33. printf("Specify index to search! alias,unino,email\n");
  34. return(1);
  35. }
  36. break;
  37. case 7:
  38. cTempPtr = argv[1];
  39. strcpy(searchField, cTempPtr);
  40. cTempPtr = argv[0];
  41. strcpy(searchValue, cTempPtr);
  42. cTempPtr = NULL;
  43. doMemOut = 1;
  44. echo = 0;
  45. break;
  46. default:
  47. printf("Invalid number of arguments!\n");
  48. return(1);
  49. break;
  50. }
  51.  
  52. if(query_ups(searchField,searchValue,"uniEduEmailForwardingAddress",email)!=0)

The goal here is to prep the incoming data for the query_ups function. The assembler arguments are as follows.

  1. ARGS DS 0F OMVS CALL ARGUMENTS ADDR LIST 00007700
  2. A1 DS A ARGUMENT 1 ADDR-> LDAP REQ KEY TYPE 00007800
  3. A2 DS A ARGUMENT 2 ADDR-> LDAP KEY 00007900
  4. AA3 DS A ARGUMENT 3 ADDR-> ADDR-> NETWRKID 00008000
  5. AA4 DS A ARGUMENT 4 ADDR-> ADDR-> UNINO 00008100
  6. AA5 DS A ARGUMENT 5 ADDR-> ADDR-> EMAIL 00008200
  7. AA6 DS A ARGUMENT 6 ADDR-> ADDR-> LDAP MESSAGE 00008300
  8. A7 DS A ARGUMENT 7 ADDR-> LDAP SERVER IP ADDRESS 00008400

As you can see, they pull in the data with strcpy originally. I'm just trying to clean it up and make it easier for the next unlucky to look at. To be honest, I don't know how the assembly program is passing the addresses, I don't have access to it. Just the above arg list that was given to me to go off of. I appreciate all of your help. I think the way it is working is basically the same way it was, so I'm assuming it is ok as is. But I guess I won't know until they test it and see if it gets the values from memory like it is supposed to.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: gnujohn is an unknown quantity at this point 
Solved Threads: 0
gnujohn gnujohn is offline Offline
Newbie Poster

Re: dreference argv

 
0
  #8
Apr 15th, 2009
/* you are going to have some trouble with your rewritten source at compile time, if you don't change your declaration of nid as a weird sort of structure node: it's a typo, no doubt. Also, you should put in some comments for the next person to show your logic, since they cost nothing. */
Last edited by Narue; Apr 15th, 2009 at 9:59 am. Reason: Removed email
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 12
Reputation: krellor is an unknown quantity at this point 
Solved Threads: 0
krellor krellor is offline Offline
Newbie Poster

Re: dreference argv

 
0
  #9
Apr 15th, 2009
Actually, NID is fine. The problem is that I use a TN3270 emulator to connect to the mainframe from Windows. What this means is that the hex code for [] in the mainframe is different than in the Windows world. So, when I copy back and forth I have to edit the hex values to get them to display correctly. Windows, not knowing how to display the hex code, just shows a period.

Also, I will definitely comment this code thoroughly when I am done. It is difficult to move blocks of text on the mainframe in oedit (or at least a lot clumsier than having a mouse) so I write as little as possible until I am sure it is correct/where I want it to be. I appreciate the tips though.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: gnujohn is an unknown quantity at this point 
Solved Threads: 0
gnujohn gnujohn is offline Offline
Newbie Poster

Re: dreference argv

 
0
  #10
Apr 15th, 2009
Good on both counts, and glad to try to help; interesting code.--John as gnujohn
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC