Find the size of pointer in C
1
Name:
Anonymous
2009-06-25 0:32
suppose char *str has some bytes(not malloced), how can i determine the size of *str without access to str itself?
2
Name:
Anonymous
2009-06-25 0:34
Pointers are all the same size in C
3
Name:
Anonymous
2009-06-25 0:39
>>2
I know, i just want to to know how long the data in pointer is.
like "abc" is three bytes "a" is one but *str doesn't shows that unless it was malloc'ed.
4
Name:
Anonymous
2009-06-25 0:43
sizeof?
5
Name:
Anonymous
2009-06-25 0:45
>>4
sizeof work on datatypes(int,char,pointer address,etc), not byte strings.
6
Name:
Anonymous
2009-06-25 0:50
~ >cat > test.c
#include <stdio.h>
int main()
{
char * str = "abc";
printf("%d\n",sizeof(str));
return 0;
}
~ >gcc test.c
~ >./a.out
4
~ >
7
Name:
Anonymous
2009-06-25 0:51
try with "asdahsdsahd\0abcdef"
8
Name:
Anonymous
2009-06-25 0:52
9
Name:
Anonymous
2009-06-25 1:12
>>7
did you even try
>>6 's code?
10
Name:
Anonymous
2009-06-25 1:19
>>9
>>6 returns the size of
pointer address
11
Name:
Anonymous
2009-06-25 1:20
>>10
Address is a pointer lawl
12
Name:
Anonymous
2009-06-25 1:21
Fucking retards. Gonna try googling it on obscure sepples forums.
13
Name:
Anonymous
2009-06-25 1:25
Protip: you can't and you're an idiot who will never be a real programmer.
14
Name:
>>6
2009-06-25 1:46
>>12
Successful trolling is successful. Don't forget to vote for me on
www.ratemytroll.com
15
Name:
Anonymous
2009-06-25 1:57
>>2
The standard does not guarantee that function pointers have the same size of data pointers.
16
Name:
Anonymous
2009-06-25 2:20
>>15
Does it mention the rationale behind that decision? Seems odd to me.
17
Name:
Anonymous
2009-06-25 2:24
>>16
Some architectures store code and data in separate address spaces.
18
Name:
Anonymous
2009-06-25 2:38
>>15
the standard doesn't guarantee that
float pointers and
int pointers are the same size either.
19
Name:
Anonymous
2009-06-25 3:27
int strlen(unsigned char *string_start)
{
/* Initialize a unsigned char pointer here */
/* A loop that starts at string_start and
* is increment by one until it's value is zero,
*e.g. while(*s!=0) or just simply while(*s) */
/* Return the difference of the incremented pointer and the original pointer */
}
20
Name:
Anonymous
2009-06-25 4:30
>>19
int strlen(unsigned char *string_start)
{ return strrchr(string_start, 0) - string_start; }
21
Name:
Anonymous
2009-06-25 4:35
>>20
actually, that should be:
size_t strlen(const char *string_start)
{ return (size_t)(strchr(string_start, 0) - string_start); }
22
Name:
Anonymous
2009-06-25 4:51
>>19-21
how can i determine the size of *str without access to str itself?
Basically, he's asking the impossible.
23
Name:
Anonymous
2009-06-25 5:00
>>22
>>19-21 doesn't have anything to do with what he was asking. that was already solved by
>>6 . we've moved on to more interesting things.
24
Name:
Anonymous
2009-06-25 6:55
>>1
Look at where you declared *str
25
Name:
Anonymous
2010-11-25 19:14