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.

How to pass by reference a dinamic array to a C routine

I have read much posts on the argument but no one
clearly says if this operation is possible or not.

Simply I have a routine which reads from a text
file some integer arrays (1 or 2D).
The first array in the file has known dimension,
but it contains the dimensions (so unknown at
compile time) of all the other arrays.

So I can allocate memory only after reading the
first array, but then I cannot bring out of my
reader routine the arrays created.

Maybe I was not so clear, all my problem is
represented by the following example.
Why neither test1 nor test2 routine can bring
out the pointer to the beginning of the array?
Printf calls never print the right values :-((

Maybe I am not so able with C...

....tanx in advance,

Morgan.

EXAMPLE BEGIN
_______________________________________________
#include <iostream>
#include <stdlib.h>

using namespace std;

void test1(int *);
void test2(int *);

//
// Main routine
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
int *a;

test1(a);
for(int i=0;i<3;i++) printf("%d ",a[i]);

test2(a);
for(int i=0;i<3;i++) printf("%d ",a[i]);

system("PAUSE");
return 0;
}

//
// First test routine to pass the array in "a"
///////////////////////////////////////////////
void test1(int *a1)
{
a1 = new int[3];
for(int i=0;i<3;i++) a1[i]=2*i;
}

//
// Second test routine to pass the array in "a"
///////////////////////////////////////////////
void test2(int *a1)
{
int *a2;

a2 = new int[3];
for(int i=0;i<3;i++) a2[i]=2*i;
a1 = a2;
}
_______________________________________________
EXAMPLE END.
Nov 14 '05 #1
7 5531
Morgan wrote:
I have read much posts on the argument but no one
clearly says if this operation is possible or not.


Despite your subject title, your code is C++ code, not C code. It is,
therefore, off-topic in comp.lang.c++.

Followups set to alt.comp.lang.learn.c-c++ only.

<snip>

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #2
On Fri, 6 Feb 2004, Morgan wrote:
I have read much posts on the argument but no one
clearly says if this operation is possible or not.

Simply I have a routine which reads from a text
file some integer arrays (1 or 2D).
The first array in the file has known dimension,
but it contains the dimensions (so unknown at
compile time) of all the other arrays.

So I can allocate memory only after reading the
first array, but then I cannot bring out of my
reader routine the arrays created.

Maybe I was not so clear, all my problem is
represented by the following example.
Why neither test1 nor test2 routine can bring
out the pointer to the beginning of the array?
Printf calls never print the right values :-((

Maybe I am not so able with C...

...tanx in advance,

Morgan.

EXAMPLE BEGIN
_______________________________________________
#include <iostream>
#include <stdlib.h>
There is no <iostream> in C language and you are missing the <stdio.h>
header for your printf. I'm not even sure if the C++ standard indicates
that using <iostream> will #include <stdio.h> as well.
using namespace std;

void test1(int *);
void test2(int *);

//
// Main routine
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
int *a;

test1(a);
for(int i=0;i<3;i++) printf("%d ",a[i]);
Since this is C language I can immediately say that this is undefined
behaviour. The three lines above do the following:

1) Create an UNINITIALIZED pointer to int called a
2) You pass some random value to test1() which has absolutely
no way of changing the value of a
3) You proceed to dereference a and still have not initialized it
test2(a);
for(int i=0;i<3;i++) printf("%d ",a[i]);
Same thing again.
system("PAUSE");
return 0;
}
If you want to initialize a with the results of a malloc in test1 you will
have to pass the address of a to test1. The function test1 will then
receive a pointer to a pointer to int.
//
// First test routine to pass the array in "a"
///////////////////////////////////////////////
void test1(int *a1)
{
a1 = new int[3];
for(int i=0;i<3;i++) a1[i]=2*i;
}

//
// Second test routine to pass the array in "a"
///////////////////////////////////////////////
void test2(int *a1)
{
int *a2;

a2 = new int[3];
for(int i=0;i<3;i++) a2[i]=2*i;
a1 = a2;
}
_______________________________________________
EXAMPLE END.


Just a style comment: This code is at best C++ code. If you want to use
things like <iostream> and new[]/delete[] then fully embrace object
oriented programming and go to the comp.lang.c++ newsgroup. If you want to
learn C programming then quit mixing C and C++ code.

When I look at our code it is like someone who writes stories but they
write them in French and Italian, changing languages mid-paragraph.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #3
Morgan wrote:

#include <iostream>
#include <stdlib.h>

using namespace std;

This is C++ code, and hence off-topic in comp.lang.c. The group you want
(in case you need another one besides a.c.l.l.c-c++) is comp.lang.c++.
Those whose answer in the first group should remove the crosspost to
c.l.c.

Brian Rodenborn
Nov 14 '05 #4
[I am reading this on comp.lang.c, please bear it in mind.]

"Morgan" <up*****@tin.it> wrote in message
news:ec************************@posting.google.com ...

....something about dynamically allocating arrays according to numbers read
from a file, providing the following code:
EXAMPLE BEGIN
_______________________________________________
#include <iostream>
Eh? I think you misspeled #include <stdio.h>
#include <stdlib.h>

using namespace std;
Eh?
void test1(int *);
void test2(int *);

//
// Main routine
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
int *a;

test1(a);
Undefined behaviour. Using an uninitialized variable.
for(int i=0;i<3;i++) printf("%d ",a[i]);

test2(a);
As above.
for(int i=0;i<3;i++) printf("%d ",a[i]);

system("PAUSE");
Working in DOS, right? ;-)
return 0;
}

//
// First test routine to pass the array in "a"
///////////////////////////////////////////////
void test1(int *a1)
{
a1 = new int[3];
Eh? I think you misspelled a1 = malloc(3 * sizeof *a1);

But even that would only help you leak memory like a sieve.

Let me explain. Your function allocates an array and stores the pointer in
a1 (by the way, you are not checking that the allocation succeeded). Because
C <off-topic>and C++</off-topic> passes parameters by value, a1 in your
function is a copy of a in main. Whatever you do to a1 does not affect the
value of a.

There are two ways around it. The ugly way is to pass a pointer to a
variable that you want to change and dereference the pointer in your test1.
The prettier way is to make test1 return the value, rather than alter it
through th eparameter.
for(int i=0;i<3;i++) a1[i]=2*i;
}

//
// Second test routine to pass the array in "a"
///////////////////////////////////////////////
void test2(int *a1)
{
int *a2;

a2 = new int[3];
for(int i=0;i<3;i++) a2[i]=2*i;
a1 = a2;
}
Same comments as above, an additional local variable does not change a
thing.
_______________________________________________
EXAMPLE END.

Nov 14 '05 #5
Morgan wrote:
.... snip ...
Maybe I am not so able with C...

...tanx in advance,

Morgan.

EXAMPLE BEGIN
_______________________________________________
#include <iostream>
#include <stdlib.h>

using namespace std;


.... snip ...

This is not C, so cannot be compiled with C. F'ups set.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
Morgan wrote:
I have read much posts on the argument but no one
clearly says if this operation is possible or not.
For those not noticing, or for whom the subject line is too long for your
client's window or any of several other reasons it might not be immediately
obvious, the subject line is:
Subject: How to pass by reference a dinamic array to a C routine
Since C does not have "pass by reference," and any one having "read much
posts" would know this, I can't see why this post showed up in comp.lang.c.

Continuing anyway ...

#include <iostream> ^^^^^^^^^^
This is not a standard C header.
#include <stdlib.h>

using namespace std; ^^^^^^^^^^^^^^^^^^^^^
This line is nothing but a syntax error in C.
void test1(int *);
void test2(int *);

//
// Main routine
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
int *a;

test1(a);
for(int i=0;i<3;i++) printf("%d ",a[i]); ^^^^^^
printf has its prototype in <stdio.h>. You did not include it, nor did you
include the C++ analog, <cstdio>. Since variadic functions need such
declarations visible, this is not legal C or C++.
test2(a);
for(int i=0;i<3;i++) printf("%d ",a[i]);
system("PAUSE");
return 0;
} // First test routine to pass the array in "a"
void test1(int *a1)
{
a1 = new int[3];

^^^^^^^^^^
syntax error. Why did you post to comp.lang.c, again?

[remaining non-C crap snipped]
--
Martin Ambuhl
Nov 14 '05 #7
Martin Ambuhl wrote:
Morgan wrote:
I have read much posts on the argument but no one
clearly says if this operation is possible or not.

.... snip ... Since C does not have "pass by reference," and any one having
"read much posts" would know this, I can't see why this post
showed up in comp.lang.c.

Continuing anyway ...
#include <iostream>

^^^^^^^^^^
This is not a standard C header.

.... snip ...
a1 = new int[3];

^^^^^^^^^^
syntax error. Why did you post to comp.lang.c, again?


Why did you fail to set follow-ups to eliminate c.l.c?

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8

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

Similar topics

5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
6
by: gp | last post by:
Hi all, I'm using Microsoft Visual C++ 6.0, I would like to see, debugging my project, all the elements of my dinamic objects.... I have a dinamic array and a STL vector and I need to know...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
10
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: ...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
9
by: =?Utf-8?B?VG9tbXkgTG9uZw==?= | last post by:
I don't know if the following is what you are looking for, but to me what you described was using a control array. If you were using vb6 that would be easy, the following articles hopefully...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.