Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8288539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:18:10+00:00 2026-06-08T12:18:10+00:00

I have a question regarding passing a variable that is a char array from

  • 0

I have a question regarding passing a variable that is a char array from one function into the next.

Here are the samples of code involved:

int main( int argc, char** argv )
{

int value = 0;

int nCounter = 0;
FILE* fIn = NULL;
char * sLine = new char[MAX_FILENAME_SIZE];
char * sFileName = new char [MAX_FILENAME_SIZE];
char * s = new char [MAX_FILENAME_SIZE];



if ((fIn = fopen(ImgListFileName,"rt"))==NULL)

{
    printf("Failed to open file: %s\n",ImgListFileName);
    return nCounter;
}



while(!feof(fIn)){

//set the variables to 0
memset(sLine,0,MAX_FILENAME_SIZE*sizeof(char));
memset(sFileName,0,MAX_FILENAME_SIZE*sizeof(char));
memset(s,0,MAX_FILENAME_SIZE*sizeof(char));
//read one line (one image filename)
//sLine will contain one line from the text file
fgets(sLine,MAX_FILENAME_SIZE,fIn);
//copy the filename into variable s
strncpy(s, sLine, strlen(sLine)-1);
//put a \0 character at the end of the filename
strcat(sLine,"\0");
//create the filename
strcat(sFileName,s);

nCounter++;


fclose(fIn);
delete sLine;
delete sFileName;
delete s;
    const int size = 60;
    char path[size] = "path";
    strcat(path,sFileName);

    printf (path);
IplImage *img = cvLoadImage(path);
detect_and_draw(img);
cvWaitKey();
cvReleaseImage(&img);
cvDestroyWindow("result");

void detect_and_draw( IplImage* img )
{


More code that isn't involved....


cvSaveImage(sFileName, img);

Now, I have tried the following:

void getFilename(char * sFileName)
{
    printf("The filename is %s\n", sFileName);
    return;
}

And then call with

char * S ="string"
getFilename(S);
cvSaveImage(S,img);

But “string” is placed into “The filename is: string”.

What can I do so that I can use sFileName, the char array, in cvSaveImage(sFileName, img)?

Thanks in advance, and if you need any further clarifications, please ask!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:18:11+00:00Added an answer on June 8, 2026 at 12:18 pm

    If I’m understanding correctly, what you have is a scoping problem. You essentially have:

    int main(/* */)
    { char sFileName[MAX_FILENAME_SIZE];
    
      /* code to initialize sFileName to contain a value */
    
      detect_and_draw(img);
    }
    
    void detect_and_draw(IplImage *img)
    { cvSaveImage(sFileName, img);
    }
    

    The problem is that sFileName is local to main() and not accessible in detect_and_draw(). You can either modify detect_and_draw() to take a second argument:

    int main()
    { /* stuff */
      detect_and_draw(img, sFileName);
    }
    void detect_and_draw(IplImage *img, const char* fn)
    { cvSaveImage(fn, img);
    }
    

    Or make sFileName a global variable declared/defined outside the scope of main() – although that’s quite often considered to be an inferior solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this question here: Passing one Dimension of a Two Dimensional Array in
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have a question regarding the passing of a map by reference. Let's consider
I'm learning JS here, and have a question regarding primitive values when passed in
I have a question regarding parsing data from Wikipedia for my Android app. I
There was a question regarding passing taking the following function template, and instantiating it
I have a question regarding P2P with flex. When passing data between two applications
I have a question regarding C++. This is my current function: string clarifyWord(string str)
I have a question about passing parameters in Tcl regarding to the following code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.