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. 7 5554
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.l earn.c-c++ only.
<snip>
--
Richard Heathfield : bi****@eton.pow ernet.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
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
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
[I am reading this on comp.lang.c, please bear it in mind.]
"Morgan" <up*****@tin.it > wrote in message
news:ec******** *************** *@posting.googl e.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.
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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
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
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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 (count = 0; count <= 1; count++) {
if (eval(f.RadioButtons.checked)) {
btnChosen = true;
}
|
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 must be an object
instead of
|
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 the fields values
at a specific position, but in the Watch windows I can only see the first
element...
|
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 (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.
|
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:
#include <stdlib.h>
#ifndef __LIST_H__
#define __LIST_H__
| |
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 passed also. I understand that this way of passing the array is by
value and if the prototype is declared as foo(int *), it is by
reference in which case the value if modified in the function will get
reflected in the main function as well. I dont...
|
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 ways of passing an array. In the following code, fun1(int
a1) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed...
|
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.
Here is a newbie mistake that I found myself doing (as a newbie), and
that even a master programmer, the guru of this forum, Jon Skeet,
missed! (He knows this I'm sure, but just didn't think this was my
problem; LOL, I am needling him)
If...
|
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 explain using control arrays in .net
Hope they help?
http://visualbasic.about.com/od/usingvbnet/l/bldykctrlarraya.htm
...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |