473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Get the length of data a pointer is pointing to

Hi,

I have code like this:

unsigned char *buf
*buf=1234567;

How I detect the the size of data buf is pointing to? (In this case is 7)

Thanks
Tran Hong Quang
Feb 22 '06 #1
1 1143
I have code like this:

unsigned char *buf
*buf=1234567;

How I detect the the size of data buf is pointing to? (In this case is 7)


there is a mistake in your code. buf is a pointer, and you assign it a value
of 1234567.
this means that 1234567 is used as an address, not as data. you should get
compiler warnings for this.

you should use
*buf="1234567";
instead.

this will cause buf to point to a string.
you can then use strlen(buf); to determine the length.
please note that this only gets you the length of the string.
if you have something like this

char mybuf[100];
char* buf = mybuf;

there is no way to get the size of that buffer by just using buf.

there are 2 things you can do:
you have to know in advance how large it will be (for example you have a
BUF_SIZE constant somewhere) or you have to get a size value from somewhere
else when mybuf is assigned to buf.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Feb 22 '06 #2

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

Similar topics

2
by: lawrence | last post by:
I had some code that worked fine for several weeks, and then yesterday it stopped working. I'm not sure what I did. Nor can I make out why it isn't working. I'm running a query that should return 3...
8
by: John Smith | last post by:
Hi, I'm writing a library in C++ which is supposed to be used by people using C. One function I have must return a string to users which is arbitrary length. The user must be able to use this...
8
by: pembed2003 | last post by:
Hi all, As an exercise, I am trying to come up with a function to count the length of a char* string using recursion. I came up with this: int count_length(char* s){ if(s == 0) return 0;...
2
by: GrahamJWalsh | last post by:
I have a basic question regarding various data types (related to reinterpret_cast, static_cast). Lets say I have some declarations thus; int i = 444; unsigned int* uip; I then do...
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
15
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float...
12
by: ur8x | last post by:
Why does this declaration give undefined result: file1: extern char * p; file2: char p; Let's assume p has been initialized, now accessing p...
31
by: RS | last post by:
Hi, Looking to see if the following construct is valid: typedef struct { int foo; char bar; } foobar; Basically, the idea is to have the structure above point to a message buffer that has...
13
by: Paminu | last post by:
I have this function: int test(void *blop) { char *sp; sp = blop; int i; for (i=0; i < LENGTH.sp; i++)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.