Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer trouble.....

Status
Not open for further replies.

Dorff

Programmer
Mar 13, 2002
72
CA
I'm having a heck of a time with this code....I think my concept of pointers is a little screwy. Maybe someone can help....

#include <stdio.h>
#include <conio.h>

void main(void)
{
int *num, num2, encrptNum, i;
num = getche();
clrscr();
for (i=0;i<strlen(num);i++)
{
num2 = num2 + num;
num++;
}
if (num2 < 10)
encrptNum = num + 2;
if (num2 > 9 && num2 < 26)
encrptNum = num + 3;
if (num2 > 25)
encrptNum = num + 24;
printf(&quot;Original number was %d\n\n&quot;, num);
printf(&quot;Encrypted value is %d\n\n\n&quot;, encrptNum);
printf(&quot; Press enter to continue...&quot;);
getche();
}

Ryan
 
autch,autch,autch...
Where can I start ?
your int * is a pointer to an integer,
but you never reserve memory for this pointer!
When you create this pointer,it contains garbage,which
means,it points to somewhere in memory,but where?,only the toes know :)
You have to reserve your memory with new. (int*num=new int;)
This &quot;new&quot; command will ask your memory where it has some space left for an int. It will return a pointer to the location in memory of that int. That address of that space comes in num.
Before you exit your program you have to release it again with free(num);

You MUST initialize your variables at creation time!
so, int *num=NULL,num2=0,encrptNum=0,i=0;
just do it,it is a must.

If you want to know how many digits your integer has, strlen is NOT the way to do it.strlen is for arrays of characters.

If you want to read in a number from the keyboard, include
iostream.h and use
cin>>num; //read number
cin.get();//read enter sign
int count=0;
for(int temp=num;temp>0;temp/=10)count++;

e.g. temp count
1234 0
123 1
12 2
1 3
0 4

I know you want to do an encryption,but what exactely do you want to do? Give more explanation pleazzze...

do you work in c++ ? I presumed that you did.
you really don't need an int* for this.



Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Themuppeteer is mostly right, but I'm gonna make one correction: when he says to release the memory with
Code:
free(num)
, he really means
Code:
delete num
...
Code:
free
can (or at least should) only be used with memory allocated with C allocation functions, and
Code:
delete
is used with memory allocated with
Code:
new
.
 
Yeah,chipperMDW is right.
with new you should use delete
and in c,you use malloc and there you use free.

(I'm curious chipperMDW,would free work also for that int* ?
Would C++ use malloc internally ?)

Sorry about that mistake :) !
Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Only to mention that
num++;
in this case makes pointer-arithmetic but not I presume you want.
I suppose you need a simple integer:
int num;
 
Themuppeteer:

I believe new is allowed to be implemented using malloc (and delete allowed to be implemented with free), but it isn't required to be. So depending on whether or not the particular C++ implementation uses malloc, a new/free combination might or might not work. Even if new does use malloc, there's no guarantee that the compiler would allow a direct call to free on new-allocated memory.

So, short answer: it'd be compiler-dependant, and probably ill-formed, but sure, it might work.
 
Also, please note that

int *num, num2, encrptNum, i;

declares num as a pointer but
num2,encrptNum and i are all integers. A common mistake I have seen in the past when I was tutoring in college was students often thought that

int* a,b,c; declared 3 int pointers.

Just keep this in mind when declaring them :)

Matt
 
A quick add to all that. If you are using new and delete for dynamic memory allocation make sure the source file is compiled using the cpp file extension. For plain old c programming use malloc and free. Personally I think new and delete are easier to use.

James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top