|
So i am trying to summarize my doubts.Correct me if I am wrong...
int *i;
static float *k;
int j;
i=&j;
k=&j;
printf("%d ,%f",*i,*k)
The procedure would be like this
compiler allocates 4 bytes each for pointer i and integer j. and when we prints i and k compiler reads byte information fromthe same or contigious addresses with respect to the datatype.
I still have doubt in this part.How compiler knows i is an integer.where the command "You should read only 4 bytes for i since its an integer" is stored.
of course we have declared like int *i and it stores only an address space.Will it throw error if we performed like this.
float q;
i=&q;
Thank you
-- modified at 5:34 Friday 26th May, 2006
|
|
|
|
|
jithAtran - ii wrote: I still have doubt in this part.How compiler knows i is an integer.where the command "You should read only 4 bytes for i since its an integer" is stored.
First, i is not an integer, it is a pointer to an integer. Second, the compiler does NOT store the information anywhere, it simply uses the information when it generates the machine code. When the compiler parses the program, it knows that i is of type int * , so it knows that whatever i is pointing at is an integer. Whenever it sees it being referenced/dereferenced, it uses that fact that i is a pointer to an integer to generate the correct code. To summarize, the information that i is a pointer to an integer is not stored anywhere, the compiler just generates code that treats i as a pointer to an integer.
jithAtran - ii wrote: Will it throw error if we performed like this.
float q;
i=&q;
Yes, it will. Like I said, at compile time, the compiler knows that i is a pointer to an integer, so it obviously won't allow it to point at a float.
Regards
Senthil
_____________________________
My Blog | My Articles | My Flickr | WinMacro
|
|
|
|
|
Thank you Senthil,
Now I think I am pretty clear about these concepts .Yeterday, I referred the book of computer organization by hamacher and pointers in C.Your explanations helped me to sum up the things logically.
yeah ..But ..only this
S. Senthil Kumar wrote: the compiler knows that i is a pointer to an integer
How?
Correct me if I am wrong,
Lets declare like
int *i;
compiler parses this.
That means i has been given an address space in the memory(1000/1001).
Now we are trying to store an address say
like
______________
__00__|___7D__ where 007D is the address of a
1000 1001 float variable.
Now here is my doubt.How compiler knows this assignment is wrong.Obviously,it is the alloted space for an intger.But how does compiler keeps track that 1001/1002 are for integer.
i am sorry,I think ,i have to still understand the concepts
Thank you.
|
|
|
|
|
jithAtran - ii wrote: That means i has been given an address space in the memory(1000/1001).
Now we are trying to store an address say
like
______________
__00__|___7D__ where 007D is the address of a
1000 1001 float variable.
Now here is my doubt.How compiler knows this assignment is wrong.
Well, you're not doing
int *p = 0x007D;
are you? You are attempting to assign the address of a float variable to an integer pointer, so that's why the assignment is wrong.
The compiler knows this because, when it compiles code, it keeps track of the type of all variables, using something called a symbol table[^]. When it's time to generate code, it sees this line
float k = 10.0;
int *p = &k;
it realizes that p is a pointer to int and k is a float and therefore generates an error. If you want to know how it gets that information from the source code, google for "Parsing and Syntax Analysis", which is a pretty huge topic in itself.
Again, the compiler doesn't know addresses 1001/1002 are for pointing to integers, rather, it only knows that p is a pointer to an integer. The absolute address of the pointer itself is determined at runtime.
Regards
Senthil
_____________________________
My Blog | My Articles | My Flickr | WinMacro
|
|
|
|
|
thanks senthil,
Now I got the clue.I was also googling to know more abou this.And I thought I am pretty clear on the topic now and took the C test of brainbench(Don't misunderstand. that was not my aim).
But Failed!!!! I got only 2.72 where the cut off mark was 2.75.
They said I was weak in pointers and functions..
Ok ...thanks for the help
[modified at 5.34 Indian Time]
Yeah..I got through this time .Thank you...
[/modified]
-- modified at 8:07 Saturday 27th May, 2006
|
|
|
|
|
In general what you say is correct. I have a minor correction to the following however:
S. Senthil Kumar wrote: the compiler allocates a certain number of bytes (depending on the platform) on the stack.
This is not always the case: the pointer need not be allocated on the stack. In fact the allocation of storage is an issue that concerns any variable and not just pointers. Consider the following:
struct Data
{
int number;
int *pNumber;
};
void Function()
{
Data IsOnTheStack;
}
Data InDataSegment;
Data *pData = new Data;
So in short pointers need not be on the stack and in fact where they are is not related to the fact they are pointers but the context.
Steve
|
|
|
|
|
Stephen Hewitt wrote: This is not always the case: the pointer need not be allocated on the stack.
Agreed. I just didn't want to confuse the OP more by attempting to explain where pointers themselves are allocated
Regards
Senthil
_____________________________
My Blog | My Articles | My Flickr | WinMacro
|
|
|
|
|
hi,
pointer holds the address of a variable.
u r saying that integer 2 is stored in 1000 and 1001 as 0000 0010.
Now p points to 1000, which is the base address of integer 2.
In the case of p=&j
j=4;
pointer will be adjusted to the address of j. Hence *p and j will prints the same value.
|
|
|
|
|
int * p ;
//"say the locations are 1000 and 1001."-it's ok..
//that means The locations 1000 and 1001 are rady to hold address
//of an integer. If write
*p=2; //means you are trying to assign integer value on the address
//which is store on 1000 and 1001.
//So it will cause acess violation.at run time.
// we can do this after assign p=&j;
p=&j;
//Then
*p=2;
//means the Now 1000 and 1001 is holding the address of "j";
//And the value of that Address will be 2;
//Now *p and j will print the same value.
Saday Chand Sarkar
Software Engineer
Trek Technology(s)Pte.Ltd.
|
|
|
|
|
Thank you.I have doubt only in the memory part..I think I understand how values are being manupulated through pointers
can we do like this...
int *p,x // allocating 4 bytes for pinter p in memory;
float k //allocating 4 bytes.
p=&k // this will work very well.
Now,
k=&p // what will happen now
or k=&x;// if k has enough space to store the address of it can hold.
But then
*k should work ,I know,which will not
Thanks
-- modified at 3:15 Friday 26th May, 2006
|
|
|
|
|
An int* pointer can not (without perverse casting) point to a float. Simple pointers like the ones you're using will always be a fixed size; on 32-bit Windows this will be 4 bytes. The actual value of a pointer (the bit pattern in memory) indicates a location in memory where some data starts; the type of the pointer tells the compiler what type of data to expect. The example you gave will not compile - BUT if it did this is what it would be saying:
Make the pointer "p" point the data for the variable "k". When the user indirects through the pointer "p" treat the data as if it was an integer.
Steve
|
|
|
|
|
Thank you stephen,
Your reply forcing me to study stack and heap concepts of memory more deeply, though I know it theoratically.It would be nice,If you can point me to some good stuffs and articles.
Thank you...
|
|
|
|
|
hi,
I was facing a problem while i was developing a code, which should run on different OS like linux and windows. The only function in linux for which i could not find a windows equivalent was gettimeofday() which returns the system time. My only requirement in windows code is that I donot want any dependency on windows libraries. So, though there exists many functions in windows libraries to retrieve the system time, they all require either windows.h or some other specific windows based headers which i dont want.
So can anybody suggest an alternate to gettimeofday() which does NOT have any dependency on windows whatsoever but is a plain c/c++ implementation.
Thanks in advance for any help.
|
|
|
|
|
use SYSTEMTIME structure or CTime
Cheers
"Peace of mind through Technology"
|
|
|
|
|
Ganesh_T wrote: use SYSTEMTIME structure or CTime
these function are not available in LINUX, i believe User want to make Multiplateform Application!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Help CRY
|
|
|
|
|
#include <time.h>
#include <stdio.h>
void main( void )
{
char dbuffer [9];
char tbuffer [9];
_strdate( dbuffer );
printf( "The current date is %s \n", dbuffer );
_strtime( tbuffer );
printf( "The current time is %s \n", tbuffer );
} Does this help you? I got this from MSDN while searching for _strtime .
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
What's the point in using gettimeofday() under linux, which is a linux specific function, if you don't want to use the equivalent under Windows? The equivalent is the GetSystemTime() function, but since it is Windows specific, you probably don't want it. The only other way would be to use the standard C library time() function, but then why don't you just use that under linux as well?
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
I am working in network programming. Its a multiclient server chat program. Each client works in saparate thread. I create thread when some client request the server to connect. I used _beginthread to create thread.
When i build the file, i get following error.
unresolved external symbol __beginthread
I have included ws2_32.lib but still it does not work.
Can you please help me?
Thanks
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
Aqueel wrote: I am working in network programming. Its a multiclient server chat program. Each client works in saparate thread. I create thread when some client request the server to connect. I used _beginthread to create thread.
When i build the file, i get following error.
unresolved external symbol __beginthread
I have included ws2_32.lib but still it does not work.
Can you please help me?
Thanks
You have to link to MSVCRT.lib or LIBCMT.lib library.
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
#if !defined(_MT)
#error _beginthread requires a multithreaded C run-time library.
#endif
Need to include the lines above in your file where you have used _beginthread and #include<process.h>
That should do the trick....
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Aqueel wrote: used _beginthread to create thread.
When i build the file, i get following error.
unresolved external symbol __beginthread
Its better if you use CreateThread Instead!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Help CRY
|
|
|
|
|
How i can clear the List box ?
How i can count the lines of a list box ?
List box
test.txt
tess.txt
test.mp3
test.doc
test.dll
now How i can say " in 3rd line is test.mp3"
|
|
|
|
|
Immunity18 wrote: How i can count the lines of a list box ?
GetCount();
Immunity18 wrote: How i can clear the List box ?
ResetContents();
Immunity18 wrote:
test.txt
tess.txt
test.mp3
test.doc
test.dll
now How i can say " in 3rd line is test.mp3"
From MSDN:
CString str, str2;
int n;
for (int i=0;i < pmyListBox-><code>GetCount()</code>;i++)
{
n = pmyListBox->GetTextLen( i );
pmyListBox-><code>GetText</code>( i, str.GetBuffer(n) );
str.ReleaseBuffer();
str2.Format(_T("item %d: %s\r\n"), i, str.GetBuffer(0));
MessageBox(str2);
}
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
thx you for the replies \o/
Hm the thing i want to do is.
When i'll doubleclick on an item then save me in a variable the number of the line
test.txt
tess.txt
test.mp3
test.doc
test.dll
if someone double click on test.mp3 it gonna say "you select 3 line"
|
|
|
|
|
Immunity18 wrote: When i'll doubleclick on an item then save me in a variable the number of the line
Inside the message handler for double click...
int nIndex = pmyListBox->GetCurSel(); nIndex now contains the index of the currently selected item.
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|