473,761 Members | 9,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"pointer to char" address restoring problem

#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(void)
{
int key = 120;
char *s = "String to Encrypt using XOR";
char *ss = s;
char echar;
char dchar;
size_t sizeofmsg = strlen(s)*sizeo f(char*);

char *emsg = (char*)malloc(s izeofmsg);
char *dmsg = (char*)malloc(s izeofmsg);

char *ddmsg = dmsg;
char *eemsg = emsg;
int lenofmsg = strlen(s);
int count=0;
int i=0;

while(count++<s trlen(s)){

echar = (char)(*ss^key) ;
emsg = &echar;
emsg++;
ss++;
}
count=0;
emsg = eemsg; // HERE when i restore the address of emsg via
eemsg, i'm not able to
// decrypt. How to restore the address of
original pointer to char ?.
i=0;
printf("\n");
while(count++<s trlen(s)){

echar = *emsg;
dchar = (char)(echar ^ key);
ddmsg = &dchar;
printf(" %c",dchar);
emsg++;
ddmsg++;
}
printf("\n");
getch();
return 0;
}
Feb 21 '08 #1
3 2351
Mahesh wrote:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
Not a standard C header; use <stdlib.hfor malloc(), realloc(),
free().
int main(void)
{
int key = 120;
char *s = "String to Encrypt using XOR";
char *ss = s;
char echar;
char dchar;
size_t sizeofmsg = strlen(s)*sizeo f(char*);
Wrong calculation.
It should be (number of elements)*(size of the type s points to).
That is,
sizeofmsg = (strlen(s) + 1) * sizeof *s;
as s points to char and sizeof (char) is guaranteed to be 1,
you can write
sizeofmsg = strlen(s) + 1;
The "+1" stems from the string terminator that is not included
in the string length but contributes to the string's size.
>
char *emsg = (char*)malloc(s izeofmsg);
char *dmsg = (char*)malloc(s izeofmsg);
Note that the cast is unnecessary and can hide an error; around
here,
T *p = malloc(NumberOf Elems * sizeof *p);
is recommended.
char *ddmsg = dmsg;
char *eemsg = emsg;
int lenofmsg = strlen(s);
int count=0;
int i=0;

while(count++<s trlen(s)){

echar = (char)(*ss^key) ;
Note that XOR encryption of the value key leads to 0 ('\0'),
which also is the string terminator.
emsg = &echar;
You probably mean
*emsg = echar;

&echar is just that, the address of echar; you do not change
the contents of the storage area you got from malloc().
So, in every iteration, you point emsg at the same object.

Note that for s, you iterate on the copy, and for emsg, you
iterate on the "original". This can lead to problems.
emsg++;
ss++;
}
count=0;
emsg = eemsg; // HERE when i restore the address of emsg via
eemsg, i'm not able to
// decrypt. How to restore the address of
original pointer to char ?.
// style comments break easily on usenet; I recommend that you use
/**/ comments which cannot change semantics or "compilabil ity" due
to line breaks
i=0;
printf("\n");
while(count++<s trlen(s)){

echar = *emsg;
dchar = (char)(echar ^ key);
ddmsg = &dchar;
printf(" %c",dchar);
emsg++;
ddmsg++;
}
printf("\n");
getch();
return 0;
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 21 '08 #2
On Feb 21, 12:37*pm, Michael Mair <Michael.M...@i nvalid.invalid>
wrote:
Mahesh wrote:
#include <stdio.h>
#include <string.h>
#include <malloc.h>

Not a standard C header; use <stdlib.hfor malloc(), realloc(),
free().
int main(void)
{
* * int key = 120;
* * char *s = "String to Encrypt using XOR";
* * char *ss = s;
* * char echar;
* * char dchar;
* * size_t sizeofmsg = strlen(s)*sizeo f(char*);

Wrong calculation.
It should be (number of elements)*(size of the type s points to).
That is,
* sizeofmsg = (strlen(s) + 1) * sizeof *s;
as s points to char and sizeof (char) is guaranteed to be 1,
you can write
* sizeofmsg = strlen(s) + 1;
The "+1" stems from the string terminator that is not included
in the string length but contributes to the string's size.
* * char *emsg = (char*)malloc(s izeofmsg);
* * char *dmsg = (char*)malloc(s izeofmsg);

Note that the cast is unnecessary and can hide an error; around
here,
* T *p = malloc(NumberOf Elems * sizeof *p);
is recommended.
* * char *ddmsg = dmsg;
* * char *eemsg = emsg;
* * int lenofmsg = strlen(s);
* * int count=0;
* * int i=0;
* * while(count++<s trlen(s)){
* * * * echar = (char)(*ss^key) ;

Note that XOR encryption of the value key leads to 0 ('\0'),
which also is the string terminator.
* * * * emsg = &echar;

You probably mean
* *emsg = echar;

&echar is just that, the address of echar; you do not change
the contents of the storage area you got from malloc().
So, in every iteration, you point emsg at the same object.

Note that for s, you iterate on the copy, and for emsg, you
iterate on the "original". This can lead to problems.
* * * * emsg++;
* * * * ss++;
* * }
* * count=0;
* * emsg = eemsg; *// HERE when i restore the address of emsg via
eemsg, i'm not able to
* * * * * * * * * * * * * * // decrypt. How to restore the address of
original pointer to char ?.

// style comments break easily on usenet; I recommend that you use
/**/ comments which cannot change semantics or "compilabil ity" due
to line breaks
* * i=0;
* * printf("\n");
* * while(count++<s trlen(s)){
* * * * echar = *emsg;
* * * * dchar = (char)(echar ^ key);
* * * * ddmsg = &dchar;
* * * * printf(" %c",dchar);
* * * * emsg++;
* * * * ddmsg++;
* * }
* * printf("\n");
* * getch();
* * return 0;
}

Cheers
* Michael
--
E-Mail: Mine is an * /at/ gmx /dot/ de * address.
Thanks you so much Michael for you invaluable reply.

-
Mahesh
Feb 21 '08 #3
Mahesh wrote:
>
On Feb 21, 12:37 pm, Michael Mair <Michael.M...@i nvalid.invalid>
wrote:
Mahesh wrote:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
Not a standard C header; use <stdlib.hfor malloc(), realloc(),
free().
int main(void)
{
int key = 120;
char *s = "String to Encrypt using XOR";
char *ss = s;
char echar;
char dchar;
size_t sizeofmsg = strlen(s)*sizeo f(char*);
Wrong calculation.
It should be (number of elements)*(size of the type s points to).
That is,
sizeofmsg = (strlen(s) + 1) * sizeof *s;
as s points to char and sizeof (char) is guaranteed to be 1,
you can write
sizeofmsg = strlen(s) + 1;
The "+1" stems from the string terminator that is not included
in the string length but contributes to the string's size.
These two idioms are both worth knowing:

ptr = malloc(NMEMB * sizeof *ptr);

ptr = malloc(STRING_L ENGTH + 1);

--
pete
Feb 21 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
20851
by: signuts | last post by:
I am wondering what it means when a pointer is aligned? Could someone perhaps enlighten me or point me in the right direction? Thank you in advance. -- Sig
3
2483
by: baumann | last post by:
hi all, i could not understand the "unnecessary" pointer comparison. /* 207 * min()/max() macros that also do 208 * strict type-checking.. See the 209 * "unnecessary" pointer comparison. 210 */ 211 #define min(x,y) ({ \
18
2129
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for last_values as well as assign the value of a pointer to the assigned space. Which I think I'm doing by using:
6
17114
by: murgan | last post by:
Hi people, i am new to this group,this is my first query, friends i want to know the difference between "function pointer" and "pointer to a function" in c lang, so friends please send the answers as early as possible, thanking u all vijay
4
6092
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple definition errors if the header file is #include'd in more than one code file. The answer was that constants have internal linkage unless declared extern, so it's OK. So, you can put something like...
4
4168
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called? class Parent { Parent::Parent() { child = new Child( this) }; Child *child; };
20
12442
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
4
10621
by: MetaKith | last post by:
Hi, Please have a look on the next code : struct foo{ float b; char z; }; struct foo foo_init = {234.77,"xcdy"}; struct foo *prt_prt=&foo_init;
4
2388
by: prasad8619 | last post by:
please help me out in this regard as this is urgent to me because i stuck in the middle of a program....... here I have a program like this...... char name="rajesh"; char *x=strchr(name,"j"); int k =strlen(name); int i=x-name; int p=k-i;
0
9333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9900
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.