LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-26-2012, 11:44 PM   #1
aihaike
Member
 
Registered: Feb 2006
Location: Paris, France
Distribution: Slackware, CentOS
Posts: 270

Rep: Reputation: 20
Question passing a 2D array to a function in C++


Dear all,

I'm new in C++ and I'm trying to translate part of my code from C.
My issue that in C I have this function
Code:
void compute_energy_cuda_ (int *atom_number_p,
                           double datain[*atom_number_p][6],double h[3][3],double g[3][3],
			   double *cutoff_p,
			   double dataout[3],
			   double fx[*atom_number_p],double fy[*atom_number_p],double fz[*atom_number_p]) {
		  
  int atom_number = *atom_number_p ;
  double cutoff = *cutoff_p ; 

....
which works fine.
But when I try to compile it with g++ I get the error:
Code:
array bound is not an integer constant before ']' token
Can someone tell me how to modify this function ?

Thanks,

Éric.
 
Old 03-27-2012, 02:36 AM   #2
velle frak
LQ Newbie
 
Registered: Aug 2011
Distribution: debian wheezy
Posts: 5

Rep: Reputation: Disabled
I'm no expert in C++, but I think the compiler complaints beacause it can not calculate the stack space needed during compile time, since the value of * atom_number_p is not known at compile time.

Maybe the solution is to pass the double datain 2D array as a (multiple) pointer and pass the length as an extra int argument?

Quote:
Originally Posted by aihaike View Post
Dear all,

I'm new in C++ and I'm trying to translate part of my code from C.
My issue that in C I have this function
Code:
void compute_energy_cuda_ (int *atom_number_p,
                           double datain[*atom_number_p][6],double h[3][3],double g[3][3],
			   double *cutoff_p,
			   double dataout[3],
			   double fx[*atom_number_p],double fy[*atom_number_p],double fz[*atom_number_p]) {
		  
  int atom_number = *atom_number_p ;
  double cutoff = *cutoff_p ; 

....
which works fine.
But when I try to compile it with g++ I get the error:
Code:
array bound is not an integer constant before ']' token
Can someone tell me how to modify this function ?

Thanks,

Éric.
 
Old 03-28-2012, 08:14 PM   #3
aihaike
Member
 
Registered: Feb 2006
Location: Paris, France
Distribution: Slackware, CentOS
Posts: 270

Original Poster
Rep: Reputation: 20
Thumbs up

Hey,

Thanks for your reply.
I figured out by just "*atom_number_p" in array declarations, like :
Code:
double datain[][6],double h[3][3],double g[3][3],
			   double *cutoff_p,
			   double dataout[3],
			   double fx[],double fy[],double fz[])
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C passing a Array of Structuers as an argument in a function dman777 Programming 5 08-22-2011 06:16 PM
[SOLVED] Passing array to thread function zak100 Programming 4 08-12-2010 11:07 AM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
passing array and variable to function in bash script ajaypitroda Programming 2 07-07-2009 11:10 PM
ARGGGH! Passing array of structs to function in C. CoderMan Programming 5 02-05-2009 10:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration