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

  • SEARCH
  • Home
  • 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 7720151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:35:31+00:00 2026-06-01T03:35:31+00:00

I have these methods. I have defined a char array in stringcontent.h file and

  • 0

I have these methods.
I have defined a char array in stringcontent.h file and that array is taken from a method in another filetest.c.

// stringcontent.h
char content1[] = "blahblah";
void  get_char_array(int which,char  *buffer){
   if(which == 1)
      buffer = content1;
      printf("%s",buffer); // prints : "blahblah"
   // and so on.....
}

// test.c
#include "stringcontent.h"
main(){
    char *buf;
    get_char_array(1,buf);
    printf("%s",buf); // prints rubbish rubbish !!!
}

I have to stick to this method interface(function signatures) and how can I fix this?

  • 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-01T03:35:32+00:00Added an answer on June 1, 2026 at 3:35 am

    If I understand, you want to get the variable using the function get_char_array. Then If I understand you need to do the following (change the get_char_function to receive a pointer to pointer to a char):

    // stringcontent.h
    char content1[] = "blahblah";
    void  get_char_array(int which,char  **buffer){
       if(which == 1)
          *buffer = content1;
          printf("%s",*buffer); // prints : "blahblah"
       // and so on.....
    }
    
    // test.c
    #include "stringcontent.h"
    main(){
        char *buf;
        get_char_array(1,&buf);
        printf("%s",buf); // prints "blahblah" !!!
    }
    

    EDIT:

    Put your declarations in the header file and implementation in the “.c” or you will have linker errors.

    // stringcontent.h
    #ifndef __STRINGCONTENT_H
    #    define __STRINGCONTENT_H
    
    void  get_char_array(int which,char  **buffer);
    
    #endif
    
    // stringcontent.c
    char content1[] = "blahblah";
    void  get_char_array(int which,char  **buffer){
       if(which == 1)
          *buffer = content1;
          printf("%s",*buffer); // prints : "blahblah"
       // and so on.....
    }
    
    // test.c
    #include "stringcontent.h"
    main(){
        char *buf;
        get_char_array(1,&buf);
        printf("%s",buf); // prints "blahblah" !!!
    }
    

    To compile (if using gcc):

    gcc -Wall test.c stringcontent.c -o test
    

    Note you don’t need to “export” content1 since the only thing you need, a pointer, is returned by a function.



    Here is another solution using strcpy. In this case you don’t need to pass a pointer to pointer.

    // stringcontent.h
    char content1[] = "blahblah";
    void  get_char_array(int which,char  *buffer){
       if(which == 1)
          strcpy(buffer,content1);
          printf("%s",buffer); // prints : "blahblah"
       // and so on.....
    }
    
    // test.c
    #include "stringcontent.h"
    #define GOOD_SIZE (100)
    main(){
        char buf[GOOD_SIZE];
        get_char_array(1,buf);
        printf("%s",buf); // prints "blahblah" !!!
    }
    

    In this case you have to take care to allocate sufficient size in buf before call the get_char_array.

    I prefer the first solution, it doesn’t need to use the strcpy. And if you don’t allocate sufficient space in the buf you can have problems. One solution is to pass the size of the buf in the function and check it before strcpy or use strncpy.

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

Sidebar

Related Questions

I have these 3 file in my program: sample1.h (method in sample1.cpp are defined
I have these two methods on a class that differ only in one method
Let us suppose i have these three methods defined: int F1(int, int); int F1(float,
I have a interface that defines some methods with attributes. These attributes need to
I have these 2 methods in the View class. the drawRect method always gets
I understand that these methods are for pickling/unpickling and have no relation to the
I have several methods that have a return type IEnumerable<T> . These methods are
I have a c++ file myCppTest.cpp which has method int myFunction(int argv, char **argc)
I have a class called Path for which there are defined about 10 methods,
Say you have these two methods: Number 1: void AddPerson(Person person) { // Validate

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.