-
need help with pointers
the first function works (int length (char a[]), but the output to the second function is -28865. I need to use pointers and pointer arithmatic.
Code:
#include <stdio.h>
#include <string.h>
int length (char a[]);
int point_size (int *a);
int main()
{
char word[]="Trying this out";
int size;
int pointer_eh;
size = length (word);
printf("The length is %d\n", size);
printf("The length is %d", pointer_eh);
return 0;
}
int length (char a[])
{
int i;
int size=0;
clrscr();
for (i = 0; i != strlen(a); i++)
{
size++;
}
return(size);
}
int point_size (int *a)
{
int i, pointer_eh;
while (*a[i] != '\0')
{
*(a+1);
i++;
}
return (pointer_eh);
}
-
if you need to use pointer math, you can eliminate all your little "++" operations that you're doing on an integer. You need to do those on a pointer instead. Additionally, use a forward slash not a back slash to end your code tags :)
edit: you got it already!
-
thanks
Yah, I can never figure out one way or another..lol
But the program still doesnt work...
-
point_eh in main is never given a value, so what you're doing is printing whatever happens to be at that memory location when the program is run. I assume that point_size is the pointer notation equivalent of length, so this would be what you want:
Code:
#include <stdio.h>
#include <string.h>
int length (char a[]);
int point_size (char *a);
int main()
{
char word[]="Trying this out";
int size;
int pointer_eh;
size = length (word);
pointer_eh = point_size (word);
printf("The length is %d\n", size);
printf("The length is %d\n", pointer_eh);
return 0;
}
int length (char a[])
{
int i;
int size=0;
/*clrscr();*/
for (i = 0; i != strlen(a); i++)
{
size++;
}
return(size);
}
int point_size (char *a)
{
int i = 0, pointer_eh;
while (*(a+i) != '\0')
{
*(a+1);
i++;
}
return (pointer_eh);
}
-
Hi,
Always remember to intialise i as it is using some junk value at this moment in this function.
int point_size (char *a)
int i, pointer_eh;
while (*(a+i) != '\0')
*(a+1);
i++;
return (pointer_eh);
secondly, can u tell me what r u exactly doing with this func.
what does this statement *(a+1); do??????????
thx
gs
-
Hi,
Always remember to intialise i as it is using some junk value at this moment in this function.
#define brs /* i donno y the flw br. is not making me sending the msgs */
#define bre
int point_size (char *a) br
int i, pointer_eh;
while (*(a+i) != '\0') br
*(a+1);
i++;
return (pointer_eh);bre bre
secondly, can u tell me what r u exactly doing with this func.
what does this statement *(a+1); do??????????
thx
gs
-
now the function point_size is coming out
The length is 0
and I am not sure what *(a+1); does
actually I just took it out and the same answer comes up...lol
-
if a is a pointer to a char, then *(a+1) is a "dereferenced" pointer to the next char in memory. So it is the char that follows *a. Get it?
-
what exactly does dereference mean
-
that means if a is at address 100 then a+1 points to 101 as a is pointer to char.
now *(a+1) will access its contents at 101.
this statement doesnt mean anything unless u do something with this.
if u r cal. size then do this
int point_size (char *a)
int pointer_eh=0;
while (*a++) /* u can also do without dereferencing it.
pointer_eh++;
return (pointer_eh);
gs
-
Code:
int point_size (char *a)
{
int i = 0, pointer_eh;
while (*(a+i) != '\0')
{
*(a+1);
i++;
}
return (pointer_eh);
}
what value has pointer_eh?
Undeclared, because you dont assign it any value.