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 564069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:43:38+00:00 2026-05-13T12:43:38+00:00

For a class, I’m writing a simple crypt function. Everything works as expected: int

  • 0

For a class, I’m writing a simple crypt function. Everything works as expected:

int crypt(char *key, int (*callback)(int, char*, int, int))
{
    int c, i = 0, n = strlen(key);
    while((c = fgetc(stdin)) != EOF)
    {
        // only change the char if it's printable
        if(c >= 32 && c <= 126)
            c = callback(c, key, n, i);

        fputc(c, stdout);
        i++;
    }
}

int encryptCallback(int c, char *key, int n, int i)
{
    return (c - 32 + key[i % n]) % 95 + 32;
}
int decryptCallback(int c, char *key, int n, int i)
{
    return (c - 32 - key[i % n] + 3 * 95) % 95 + 32;
}

Using the professor’s test case, everything works. But when I initially wrote the callbacks, I ommited the return. They were coded up like:

int encryptCallback(int c, char *key, int n, int i)
{
    c = (c - 32 + key[i % n]) % 95 + 32; // no return
}
int decryptCallback(int c, char *key, int n, int i)
{
   c = (c - 32 - key[i % n] + 3 * 95) % 95 + 32; // no return
}

When I ran the code with the non-returning callbacks, the output was still correct when I applied the test case (and yes, I recompiled, I wasn’t running the old code). I only noticed the ‘error’ when I compiled with -Wall.

So I’m confused. Why is c (in crypt()) getting the correct value after it is assigned to the return value of callback (when callback doesn’t return anything)? c isn’t a pointer, it’s just a regular int.

P.S. The assignment has nothing to do with function pointers.

  • 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-05-13T12:43:39+00:00Added an answer on May 13, 2026 at 12:43 pm

    Failing to use return in a function that has a return type other than void is undefined behaviour, if the caller tries to use the return value of the function.

    In this case, the undefined behaviour you were getting apparently was “it returned the value I wanted it to, anyway”. You just got (un)lucky. (It has nothing to do with function pointers, though).


    If you are compiling on x86, then the underlying reason it worked out is that most x86 calling conventions specify that a return value of type int is returned in the %eax register. In your case, the compiler also decided to use this register to calculate the new value of c – so the value just happened to be left there to reappear as the “return value”. If you’d done some more calculations in the function after calculating c, you’d have seen something else.

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

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think this may be what you are looking for:… May 13, 2026 at 9:13 pm
  • Editorial Team
    Editorial Team added an answer I'd probably go for the DB approach. The speed difference… May 13, 2026 at 9:13 pm
  • Editorial Team
    Editorial Team added an answer What you have looks correct, unless you haven't assigned a… May 13, 2026 at 9:13 pm

Related Questions

For example suppose I have a class Vehicle and I wish for a subclass
For a while I had been including an entire class inside of a Ruby
For a newsroom system I have a class that contains a single news story.
So for a class I have to turn in my Rails application to my
I'm looking for a class where I can override a method to do the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.