473,398 Members | 2,812 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,398 software developers and data experts.

passing 2d array and realloc

i'm new to c and have gotten my program to run but by passing hardcoded
array sizes through the routines, i want to make the array extendable by
using realloc but i'd be doing it two routines down, i need to figure out
how to pass the array such that i know how many elements are in it before i
realloc and that the array gets passed back up the routines in tact. i've
used sizeof but it only works in the first routine, the 2nd and 3rd routines
result in a size of 4 (presumably the size of the pointer to the array) and
how would i free the realloc'd memory?

if anyone has any ideas it would be appreciated.

thanks

cut down versions below
int IsSMTP( void ) //main routine
{
char Routine[] = "IsSMTP";
BOOL ReturnValue = FALSE;
int TagFieldCount = 1;

char TagField[MAX_ARRAY][MAXENVVALUE];

// build the tag field array
TagFieldCount = BuildArrayFLD( TagField, "SMTP" );

return( ReturnValue);
}

int BuildArrayFLD( char Array[MAX_ARRAY][MAXENVVALUE], char *BuildCategory )
//called by issmtp
{
char Routine[] = "BuildArrayFLD";
int ReturnValue = 0;
char BuildView[] = "BFLD";
char BuildField[] = "server";

ReturnValue = BuildArrayFromViewCategory( Array, BuildView, BuildCategory,
MAX_FLD, BuildField );
return( ReturnValue );
}
int BuildArrayFromViewCategory( char Array[MAX_ARRAY][MAXENVVALUE], char
*ViewName, char *Category, int ArrayMax, char *FieldName )
{
char Routine[] = "BuildArrayFromViewCategory";

//this is where the array values are currently set and it would be where it
would be extended via realloc

}
Nov 13 '05 #1
2 7891

Newsgroup Posting ID <ne****************@invalid.bigpond.com> wrote in
message news:2j********************@news-server.bigpond.net.au...
i'm new to c and have gotten my program to run but by passing hardcoded
array sizes through the routines, i want to make the array extendable by
using realloc but i'd be doing it two routines down, i need to figure out
how to pass the array such that i know how many elements are in it before i realloc and that the array gets passed back up the routines in tact. i've
used sizeof but it only works in the first routine, the 2nd and 3rd routines result in a size of 4 (presumably the size of the pointer to the array)
Good guess. When you pass an array to a function, it 'decays'
into a pointer to its first element. You'll need to pass the
array size as an additional argument (Use type 'size_t').

void foo(int arr[], size_t elems)
{
printf("%lu\n", (unsigned long)elems);
}

int main()
{
int a[10] = {0};
foo(sizeof a / sizeof *a); /* prints 10 */
return 0;
}

If function 'foo()' were to resize the array with 'realloc()'
then you'd need to pass back this new size to the caller so
it knows the new size. You could do this with a return value,
or a pointer to the new size (as a parameter).
how would i free the realloc'd memory?


Use 'free()'

-Mike

Nov 13 '05 #2
On Wed, 17 Sep 2003 12:19:10 GMT, "Newsgroup Posting ID"
<ne****************@invalid.bigpond.com> wrote:
i'm new to c and have gotten my program to run but by passing hardcoded
array sizes through the routines, i want to make the array extendable by
using realloc but i'd be doing it two routines down, i need to figure out
how to pass the array such that i know how many elements are in it before i
realloc and that the array gets passed back up the routines in tact. i've
used sizeof but it only works in the first routine, the 2nd and 3rd routines
result in a size of 4 (presumably the size of the pointer to the array) and
how would i free the realloc'd memory?
You can't legally realloc() a pointer which was formed, as yours is,
from an actual (declared) array object, only a pointer to allocated
space, that is one which was obtained from malloc or calloc, or a
previous realloc, and not free'd or invalidated by a previous
(successful) realloc.

A function parameter declared as an array of T actually is (rewritten
as) a pointer to T; even if you specify the bound, as you did, it is
ignored. This applies only immediately to the parameter; if you
declare a 2D array (array of array), it is really pointer-to-array.
Since an array used as an argument in the call, or indeed almost
anywhere else, decays to a pointer, this matches the parameter.
As you use in some cases in your snipped code but not others.

See any decent textbook or tutorial or sections 6 and 7 of the FAQ, at
the usual places and http://www.eskimo.com/~scs/C-faq/top.html .
//this is where the array values are currently set and it would be where it
would be extended via realloc

// comments are not standard in C90, which almost all implementations
still implement, although an easy and fairly common extension; and a
bad idea in Usenet postings anyway as you can see above.

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #3

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

Similar topics

18
by: Dan | last post by:
hello, I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete. I am trying to find and delete the duplicate in an array, thanks ...
14
by: Sameer | last post by:
Hello, i wish to read a file of int and store into an array dynamically... the size of memory allocated finally, should just be sufficeient to store n integers. I do not know the number of...
9
by: Colin Doig | last post by:
Hi, I need to pass an array of strings to a function, but I can't get it to work. This is what I wrote : #include <stdio.h> void a_function(char *blah) { printf("%s %s %s", blah, blah,...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
3
by: Thomas Christmann | last post by:
Hi! Sorry for the weird topic, I don't know how to describe it better... I have a little problem here I can't wrap my mind around. If I do: ------------------------------------- #define DWORD...
8
by: Magix | last post by:
Hi, I would like to use array of array to store different string, or if you have better idea to store different string in array index. How can I achieve that ? Any example will be greatly...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
5
by: Andreas Schmitt | last post by:
Hi, I recently worked on an open source project and tried to make on of the arrays they are using dynamically allocated to get rid of the max size. I used the realloc instead of the usual C++...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.