Thread: String fill by function

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    12

    String fill by function

    Hi, i am stack for hours on basic staff so I decided to ask for help.

    I need to fill variable buffer2 from funkcion GetBackString ... and buffer2 it need to be a array.

    Code:
    char GetBackString(){
        return "StringFromFunkcion";
    }
    
    
    int main()
    {
        char buffer[1024] = "String";
        printf("%s", buffer);
    
    
        char buffer2[1024] = GetBackString();
        printf("%s", buffer);
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,414
    Pass the array as a parameter.
    Code:
    void GetBackString(char *buffer){
        strcpy(buffer,"StringFromFunkcion");
    }
     
     
    int main()
    {
        char buffer[1024] = "String";
        printf("%s", buffer);
    
        char buffer2[1024];
        GetBackString(buffer2);
        printf("%s", buffer2);
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    12
    @Salem: Works ... thx

  4. #4
    Registered User
    Join Date
    Nov 2020
    Posts
    12
    So .... with your solotion a create a simpli code for testing Pipes ...


    Transfer data between two programs ...


    It is Works!


    But ... with void functions instead of a function with a return value is a trabl. When call in cycle with low delay > Number do not change.

    viz. screen

    https://cboard.cprogramming.com/imag...AAAElFTkSuQmCC

    run with this param "Exc_05-2writePipe 10 300000"
    Exc_05-2writePipe.c


    just run
    Exc_05-2readPipe.c

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,157
    Next time, just post in code tags like you did in post #1: 92 lines isn't so many that it needs to be attached. Try to come up with the smallest and simplest (compilable) program that demonstrates the problem and post that. If your code is so large that you must post it as attachments, it is also likely to be so large that casual readers aren't so likely to want to wade through it for free.

    You should remove this from lineWithNumberAndSum because in this context you only want to seed the PRNG once, especially since time-based seeds could end up repeating the pseudorandom sequence if you keep re-seeding the PRNG within the granularity of the time, and this can result in "Number do not change" effects:
    Code:
    srand((unsigned) time(&t));
    This looks like unnecessary use of rand() on each iteration:
    Code:
    for(int i=0;i<rand() % 20;i++)
    if you want to loop a random number of times in the range [0, 20), then I'd suggest:
    Code:
    int num_iterations = rand() % 20;
    for(int i=0;i<num_iterations;i++)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Next time, just post in code tags like you did in post #1: 92 lines isn't so many that it needs to be attached. Try to come up with the smallest and simplest (compilable) program that demonstrates the problem and post that. If your code is so large that you must post it as attachments, it is also likely to be so large that casual readers aren't so likely to want to wade through it for free.
    OK, I'm sorry I don't know the local customs ...

    Thank you for the explanation and advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-09-2017, 01:24 AM
  2. Replies: 2
    Last Post: 05-28-2015, 10:01 AM
  3. Replies: 5
    Last Post: 04-03-2013, 10:03 PM
  4. Error with function that attempts to fill an array
    By RRTT in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2011, 10:07 PM
  5. Question regarding the fill function.
    By ChristianTool in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2005, 09:08 PM

Tags for this Thread