Monday, May 24, 2010

I think I'm misunderstanding this line of code in C++?

What does this line do?





int* addr = new int[num];





It is making an array with a pointer? Or something? I've been studying for a final and doing my last programs in a class all day and the code is starting to blend together...

I think I'm misunderstanding this line of code in C++?
addr is a pointer to an array of num ints





This lets your program dynamically allocate the memory for the array, instead of having a fixed amount of memory by defining int a[1000], for example. You can't say int a[num]; it's not valid.





You use this when you'll be determining the size of the array programmically. It's then also common to see delete a[ ] later on to free the memory.





Check out this site:


http://www.fredosaurus.com/notes-cpp/new...
Reply:It creates a new array of num integers, and sets addr to the address if the first element of the array.


How can i go backwards in a character buffer in 'C'?

for example...if i hav to search for a "//" in a buff...


{


//asnbvfv hjegfub hgdyfgh cjs


}


and if my pointer is pointing to cjs.... how can i get back to //...???


thanx in advance

How can i go backwards in a character buffer in 'C'?
Decrease the value of the pointer (for example, by using the -- operator) and it will go to the left. Increase it and it will go to the right.
Reply:decrement your pointer-- and it will move back one char,


then check the dereferenced char value of where it's pointing now and the place it was pointing





do{


pointer--;


}


until ("/" == pointer[0] %26amp;%26amp; "/" == pointer[1] )





something like that, don't lock it up in some infinite loop...
Reply:Yes, you can decrement and increment the pointer as much as you want, provided the buffer still contains the characters, i.e. if they haven't been popped off a stack or something similar. In the case you're describing, you can simply set the pointer offset to zero to access the first character.





But perhaps you mean you're getting characters one by one from the keyboard or a file and want to go back and look at previous characters? In that case, as they come in send the characters to your own buffer which you can examine at will.





Bear in mind that pointer arithmetic is always a minefield and indexing something outside a buffer's valid range is a very common error.


Am trying to trace a quote by an old "preacher" - C M Lochridge {?} "This is my King"?

A pointer to a web site or such would be appreciated.


This could be circa1950's

Am trying to trace a quote by an old "preacher" - C M Lochridge {?} "This is my King"?
S. M. Lockridge - Awesome!





http://www.ignitermedia.com/products/iv/...
Reply:Took a bit, but it's SM Lochridge. Here are the google results:





http://www.google.ca/search?hl=en%26amp;client...





Enjoy!
Reply:Dr. S. M. Lochridge


How do you declare an array in a structure in C?

Is this right?





typedef struct listStruct


{


char word[20];


int counter;





pointer ptr;


} list;

How do you declare an array in a structure in C?
That works. If you don't know how large the array will be, you could also use "char*" (a pointer), and then later use malloc() to get memory for the array (as in "word = (char*)malloc(NumOfCharacters*sizeof(cha... As you have it written, you just need to make sure never to try to put 21 characters in the array. :)
Reply:yes its right 10/10

magnolia

How to fill an array in a function with c++?

how to pass the parameters?


is there any other way without pointer and please help me because I need a function that fill an array

How to fill an array in a function with c++?
Pass it normally, using the correct declaration. That is, if you had char someVar[], pass it to the function as char functionVar[]. Then treat the function argument as you would the real array.





%26gt; is there any other way without pointer


Read http://c-faq.com/aryptr/index.html . There's an equivalence between pointers and arrays in some cases, so that's why you can work with the formal argument like you would with a pointer. Go through the C FAQ I linked to. It's confusing, and takes time to digest.





If you really want to understand C++, you should get a serious book. For C++, it's C++ Primer by Lippman or Accelerated C++ by Koenig (both are suitable for beginners). To understand the C portion of C++, you may want to get yourself K%26amp;R's The C Programming Language.
Reply:you can't modify an array if you pass by value into a function you have to pass by referance. When you pass by value a copy of the array(or any object) is put on the stack( or heap) . The keyword here is copy. I'm sry but you have to pass by reference otherwise you break the rules of encapsulation.


How to convert byte into string in c language?

i m having pointer so structure which members are of BYTE type.........i want to convert them into string(to print vendor name) and into number(to print serial number).....how to do it...i m using vc++........

How to convert byte into string in c language?
Byte is equivelant to unsigned char in C++ with values (in binary) from 0000 0000 to 1111 1111. To convery byte into a string you would end up with a single unsigned char (simply by typecasting it). Not the ideal way to do this.
Reply:I don't understand what you mean. Is each letter a character type? Do you need to transfer that into one string? Or is it an array of bytes?


Why would you use reinterpret_cast in c++?

I think it's used to convert a pointer of one type to another by performing a "binary copy". What does that mean and why would you ever want to do that?

Why would you use reinterpret_cast in c++?
The reinterpret cast converts one pointer type to another. It does not perform a binary copy of the object.





Its best use is to convert a base pointer to a class back to the derived class type. for example:





class A


{


};





class B: public A


{


public:


int m_nVar;


};





// base pointer pointing to derived class


// has no access to m_nVar


A *aPtr = new B;





// cast base pointer back to derived class pointer


// has access to m_nVar


B *bPtr = reinterpret_cast%26lt;B%26gt;(aPtr);





You can also use it to cast char* to int*, or any other number of unsafe converstions, but this is not recommended.