How can we allocate more memory for an array?
I have a ar[10] and I want to grow it to ar[100] in runtime.
How can we allocate more memory for an array?
I have a ar[10] and I want to grow it to ar[100] in runtime.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
You can't do this with an array, you need to use a dynamically allocated pointer using malloc and realloc. Arrays can never change in size.
As I guessed, tnx.
But I thought it maybe possible, because arrays are pointers in theory.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
> But I thought it maybe possible, because arrays are pointers in theory.
No, they're not.
http://c-faq.com/aryptr/index.html
I read the faq and used a pointer instead of an array it worked fine. But I read something there that really confused me:
If so why this code prints out "y b", as I expected ??Originally Posted by FAQ
Code:int main() { char* cp="xyz"; char ca[]="abcd"; printf( "%s, %s\n", cp+1, ca+1 ); ; return 0; }
Last edited by siavoshkc; 01-28-2006 at 04:55 AM.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
FYI, this is the C board, not the C++ board.
Is
cout<< *(cp+1)<<" "<< ca[1] <<endl;
equally confusing?
Read the FAQ again, especially the bit about equivalence. Note in particular that "equivalence" does NOT mean "the same as". Once you get past "pointer arithmetic vs. subscripts", the differences become apparent.
Here's something else
Code:#include <stdio.h> #include <string.h> int main ( ) { char *cp ="this is a long string"; char ca[]="this is a long string"; printf( "%s, %s\n", cp, ca ); /* array decays to pointer-to-first element */ printf( "%s, %s\n", cp+10, ca+10 ); /* either can use arithmetic */ printf( "%s, %s\n", &cp[10], &ca[10] ); /* either can use subscripts */ printf( "%u %u\n", sizeof(ca), strlen(ca) ); /* sizeof does NOT decay array to a pointer */ printf( "%u %u\n", sizeof(cp), strlen(cp) ); /* sizeof on a pointer is different */ return 0; }
I thought the only difference is that compiler knows that array points to a static size block of memory but ptr can points to anywhere.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Arrays are arrays, they don't point somewhere else.
char ch; // a single ch
char ch[2]; // two chars, in successive memory locations
>> it emits code to start at the location ``a'', move three past it <<from FAQ
Three past of it? Why? When we can notice?
Why there should be difference between ptr and array?
Array is a sequence of bytes, the first one is addressed in arr, but compiler keeps in mind that this pointer points to an array so if you put a bigger number in subscription it will go to a illegal place (ex. arr[7]).Code:char arr[]="abcd";
Look at this code
OUTPUT IS THE SAME!!!Code:int main() { char ca[]="abcd"; char* cp=ca; printf( "%s, %s\n", cp+1, ca+1 ); return 0; }
It means ca acts exactly like a pointer, so what is the difference?
Last edited by siavoshkc; 01-28-2006 at 09:15 AM.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Apparently, you didn't run my code to discover the difference in the value of sizeof()
When the output generated from the code is examined. Some pseudo-assembly might be something like this.Originally Posted by siavoshkcCode:load value,[array+3]Code:load a,pointer load value,[a+3]Because an array is an array and a pointer is a pointer. Why should there be a difference between a car and a truck if they go can get you to the same place? Because they do different things.Originally Posted by siavoshkc
Try to keep reading that FAQ until it makes sense.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I knew they differ. But I think the difference is because compiler knows where the ca points to(an array) and looks at the definition of the array then returns a value for size.Originally Posted by Salem
Last edited by siavoshkc; 01-28-2006 at 10:41 PM.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Is this right:
array is not a pointer. when we writeca actaully is the first element of the array(not a pointer to it). But cp returns the address of the element it points to. I mean there is no other place in memory to keep the address of an array. For example if we use an array[3] of char, we will have |____|____|____| ==3 bytes.Code:printf( "%s, %s\n", cp+1, ca+1 );
But if we use pointer we will have |____|____|____|____| ==32bit address for the pointer
and |____|____|____| ==3 bytes.
By jove, I think he's got it!
What about two dimentional arrays?
ar[] is pointer, ar[][] is value?Code:int ar[x][z];
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C