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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:12:37+00:00 2026-06-04T11:12:37+00:00

I am new to C and have hit a problem with an example form

  • 0

I am new to C and have hit a problem with an example form K&R C ( section 1.9) not working properly. Here is the code I have copied from the example, and gone over once looking for discrepancies:

#include <stdio.h>
#define MAXLINE 1000

int mygetline(char line[], int maxline);
void copy(char to[], char from[]);

// print longest input line

main() {
    int len;
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];

    max = 0;
    while ((len = mygetline(line, MAXLINE)) > 0)
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0) // there was a line
        printf("%s", longest);
    return 0;
}

// getline: read a line into s, return length
int mygetline(char s[], int lim) {
    int c, i;

    for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}

// copy: copy 'from' onto 'to'; assume to is big enough
void copy(char to[], char from[]) {
    int i;

    i = 0;
    while ((to[i] = from[i]) != "\0")
        ++i;
}

When I compile I get the following:

cc -Wall -g test.c -o test
test.c:9:1: warning: return type defaults to ‘int’ [-Wreturn-type]
test.c: In function ‘copy’:
test.c:45:30: warning: comparison between pointer and integer [enabled by default]
test.c:45:30: warning: comparison with string literal results in unspecified behavior [-Waddress]

When I run the program, this happens:

j

on@jon-G31M-ES2L:~/c$ ./test
Hello, does this work?
Segmentation fault (core dumped)

I am using gcc as my compiler.

  • 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-04T11:12:39+00:00Added an answer on June 4, 2026 at 11:12 am

    The problem lies in the below line:

    while ((to[i] = from[i]) != "\0") 
    

    It should be

    while ((to[i] = from[i]) != '\0') 
    

    Now, as to why the first one was wrong. “\0” is actually a string literal which is stored in some memory (let’s say the address is 0x7d5678 just for example), whereas the left side of != is an integer. The compiler automatically knows that such a comparison is wrong and can never pass, so it gives you the warning

    warning: comparison between pointer and integer [enabled by default]
    

    Also, when you have a string literal in your code, you have no control as to, at what address will it be stored, so the compiler again knows that any comparison with a string literal is wrong hence flags the warning

    warning: comparison between pointer and integer [enabled by default]
    

    Also, as an additional note, always remember that strings in C can be compared & manipulated only using the strcmp or strncmp group of string functions. You can never do an “=” comparison on a string.
    Hope this helps in understanding.

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

Sidebar

Related Questions

I'm new to python and have hit a problem with an SQL query I'm
I have new problem. My code: .method public static void Main() cil managed {
I'm new to Vim (almost new) and have some questions. The problem came from
Ok so here is the problem, I found a nice working 2D Camera from:
I thought I understood EDQ until I hit this problem. I have the code
Here's my problem: I have server A, running node.js and using socket.io for communicating
I've run into a problem that I'm certain others have hit and solved. In
I am trying to learn ASP.NET MVC and I hit this problem: I have
Looks like operator new and operator new[] have exactly the same signature: void* operator
i have new webcam. The software of the webcam can give out the webcam

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.