Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resize one-dimensional array

Status
Not open for further replies.

FurryGorilla

Technical User
Apr 11, 2001
76
GB
Hi

I'm having a little trouble finding any information on exactly how to do this. I merely need to resize a one dimensional array depending on another number.

I have 2 arrays of floating point numbers named 'buffer1' and 'buffer2' and these are to be resized using the integer 'size':

buffer1[size];
buffer2[size];

I've had a look at information on malloc and realloc but can't work out how to do it. I imagine I'm going wrong with pointers but any help would be greatly appreciated.

Thanks
Chris
 
This is a simple example using strings. The random swap
thing was just to liven it up ;).
The same principle works for your float arrays as long
as they are dynamically allocated or NULL.
Otherwise realloc will NOT work.
Arrays are not pointers.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SEGSIZE 4
/*globals*/
char garbagestring[] = &quot;andjjdf7e4jwi9d9ern3uidf8frne4ufyuf&quot;;

/*function protos*/
int resizeArr(char *, int);
int repopArr(char *, int);


int main(void) {
int y = 0;
char *mystr = malloc(1 * SEGSIZE * sizeof(char));

              strcpy(mystr,&quot;poo&quot;);
              resizeArr(mystr,6);
              printf(&quot;%s, length = %d\n&quot;, mystr, strlen(mystr));
             
              resizeArr(mystr,16);
	      printf(&quot;%s, length = %d\n&quot;, mystr, strlen(mystr));

free(mystr);
return 0;
}

int resizeArr(char *mstr, int sz) {
 mstr = realloc(mstr,sz * sizeof(char));
 repopArr(mstr,sz);
 
     if (mstr) {
         return 0;
      }

return 1;
}

int repopArr(char *str, int s) {
int i, x, p;
           
           for (i=0 ; i < s ; i++) {
             printf(&quot;At %d reading %c\n&quot;, i, str[i]);
             if (!str[i]) {
                for (x=i , p = strlen(garbagestring) ; x < s ; x++) {
                    str[i] = garbagestring[(int)(1 + rand() % p)];
                    printf(&quot;Adding %c\n&quot;, str[i]);
                }
                str[i + 1] = '\0';
              }
            }
return 0;
}



 
Hi all:

So, arrays are not pointers?

I allways think that they was, in fact, the name of an array is a constant pointer to its first position.

array[0] == *array
array[5] == *array+5

Am I wrong? Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Arrays and pointers are NOT the same though they work very similarly in most situations.

You can normally use array names as if they were
pointers. You can use an index with a pointer.
Most compilers automatically convert a one dimensional
array to a pointer to the first element of the array.
Exceptions occur when using the address of(&)operator and
when using sizeof().

There was an interesting thread on this about what the
pointer: char ***name was representing. At least three
different, IMO acceptable, opinions were presented.
 
Yes arrays are pointers. What marsd was referring to was the difference between an &quot;allocated&quot; array and a &quot;stack&quot; based array. You can't use realloc() on a stack based array.

-pete
 
thanks all. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
So monkey, do you actually need to change the size of the elements of your array or change the actual number of elements in the array? If it is the latter, then you can use calloc(numEls, elemSize) to dynamically allocate space for an array at run-time.

Let me know what happens.

Sam
 
Marsd, char *** can be called an array of string arrays, no? What else can it be known as?
 
Sorry I've taken so long to get back in contact. Had a bit of a problem with net access. The way I did it in the end was using:

buffer1 = (float *)calloc(size, sizeof(float));
buffer2 = (float *)calloc(size, sizeof(float));

Thank you for all the followups.

Much appreciated
Chris
 
>> char *** can be called an array of string arrays, no?
>> What else can it be known as?

How about a pointer to an array of strings?

as in:
Code:
char* names[] = {&quot;Bernie&quot;,&quot;Tom&quot;,&quot;Mack&quot;,&quot;Sarah&quot;};
void getStringArray( char*** ptrNames){

	*ptrNames = names;
}
void main(){
	char** ptr;
	getStringArray( &ptr);
	for(int n=0; n<4; n++)
		cout << ptr[n] << endl;
}

-pete

 
See comp.lang.c's faqs for more info on how pointers
and arrays, and pointer notation can be correctly
interpreted if you are really interested.
Every C programmer should read that list once in a
while.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top