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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:59:18+00:00 2026-05-18T10:59:18+00:00

Spoiler: I am an absolute beginner to C. I quickly threw threw together this

  • 0

Spoiler: I am an absolute beginner to C. I quickly threw threw together this program to test my knowledge, but my compiler is giving me errors. What is the problem and why?

#include <stdio.h>

void main()
{
    char *string = "abcdefghi";

    printf("%s\n\n", string);

    printf("%s\n\n", substr(string, 1, 2));
}

char * substr(char *string, int start, int length)
{
    int i;
    char *temp;

    for(i = 0; i < length; i++)
    {
        temp[i] = string[i+start];
    }

    return temp;
}

EDIT:

Sorry, it’s like 1 AM here, I’ve been up trying to figure this out.

The errors are:

main.c: In function ‘main’:
main.c:9: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
main.c: At top level:
main.c:12: error: conflicting types for ‘substr’
  • 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-18T10:59:19+00:00Added an answer on May 18, 2026 at 10:59 am

    Here are the errors I see:

    Use of uninitialized pointer

    In substr you declare char *temp; and then use it without initializing it to anything. This is not a compile-time error, but this program will almost certainly crash when you run it, since temp will effectively point to a random memory address. This is a case of undefined behavior, and C is chock full of it. Undefined behavior will come out of nowhere and eat your pets if you aren’t careful.

    Consider malloc()ing some memory, or having your function receive a pointer to a buffer where it can write the portion of the string.

    Use of function not yet declared

    In C you must declare functions before they are used, or at least declare their prototype. Above main()‘s declaration, add this line:

    char * substr(char *string, int start, int length);
    

    No use of const where it makes sense

    When assigning a string literal to a char*, that variable should be declared const. So change

    char *string = "abcdefghi";
    

    to

    const char *string = "abcdefghi";
    

    You will have to change your function prototype to

    char * substr(const char *string, int start, int length)
    

    which is what it should have been in the first place.

    Added 2010-12-02:

    substr() does not add terminating null character

    The substr() function, while algorithmically correct in every other sense, does not add a terminating null character to the new string. This will cause printf() and every other string-using function (like strlen(), strcpy(), etc.) to run off the end of the string into unallocated heap memory, or stack memory (depending on how you resolve the “uninitialized pointer” issue).

    To fix this, add this line immediately after the for loop, and before the return statement:

    temp[i] = '\0';
    

    Note that this should not be added in the for loop, as that would have the effect of creating a string with zero length.

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

Sidebar

Related Questions

No related questions found

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.