CHAR POINTER

Jul 2, 2010 at 8:31am
Hi Guys

I have a small doubt on the char pointers.
I have a case where the char pointer points to the start of a memory address


int* ptr--> -----------------
| |
| |
| |
| |
| |
----------------- <--- Need to get this address

Now the situation is , I dont know the size of the memory address, then how can I go to the end of the memory address
Jul 2, 2010 at 8:41am
What you have to do is either store how big the array is, or you need to put a special value in the array that marks it as the end position.

The convention in C for normal strings of characters is to make the last memory location contain a zero.

int* ptr--> -----------------
H
E
L
P
0 <- zero
----------------- <--- Need to get this address

Jul 2, 2010 at 8:44am
I'm not sure if I understand your problem correctly. If i'm right, this should help
1
2
3
4
5
6
7
8
9
10
11
int i=0;
char tab[]="text";
char * ptr=tab;
do
{
	ptr++;
	i++;
}
while(*ptr!=NULL);
i++;
cout<<i<<endl;
Last edited on Jul 2, 2010 at 8:55am
Jul 2, 2010 at 12:00pm
closed account (N85iE3v7)
If this pointer was dynamically allocated, you may already have its size, for ex:
1
2
3
4
5
6
7
8
   void f(int n)
  {
     char* ptr = new char[ n ];// if you are using C malloc would be used instead
     // lots of code start from here
     // then you could just go and use n to find out where the last position
     // allocated was      
     char c = *( ptr + n )
  }


In case you have a C string, then the solution above given by mtweeman might work, or use the C library called strlen. It returns the size of the string minus the 0x00 terminator.

As you said, you don't know its actual size. Is your data coded using TLV-BER encoding? I once worked in a project where dynammic allocation could not be used, but they had a pointer for a buffer that actually was used to go through a large array of data and the only way of knowing how to go through it was by using the TLV specs.

It is rather unsafe to pass a buffer without its size though. If you wrote the code that creates this char pointer buffer, perhaps you might have a way of finding its size. If you are dealing with a third part library ( which i think it might be your case ) that only returns a buffer, I think, that they might have added a 0x00 char at the end of the buffer, or there might be a char to delimitate its end just as Galik said.

Last edited on Jul 2, 2010 at 12:01pm
Topic archived. No new replies allowed.