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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:53:06+00:00 2026-06-09T21:53:06+00:00

I’m doing Zed Shaw’s Learn C The Hard Way course. On exercise 11, in

  • 0

I’m doing Zed Shaw’s Learn C The Hard Way course. On exercise 11, in the extra-credit question #2, he askes:

  1. Make these loops count backward by using i– to start at argc and count down to 0. You may have to do
    some math to make the array indexes work right.

  2. Use a while loop to copy the values from argv into states.

I try:

#include <stdio.h>

int main(int argc, const char *argv[]){

    int i = argc - 1;
    while(i >= 0){
        printf("arg %d: %s\n", i, argv[i]);
        i--;
    }

    char *states[] = {
        "California", "Oregon",
        "Washington", "Texas"
    };

    int num_states = 4;
    i = num_states - 1;
    while( i >= 0){
        printf( "state %d, %s\n", i, states[i]);
        i--;
    }

    i = 0;
    while(i < argc && i < num_states){
        int j = 0;
        while( (states[i][j++] = argv[i][j++]) != '\0' ){
            i++;
        }
        states[i][j] = '\0';
    }

    i = num_states - 1;
    while( i >= 0){
        printf( "state %d, %s\n", i, states[i]);
        i--;
    }

    return 0;
}

I get a Segmentation Fault. I understand that you can’t copy arrays in C, that they are const pointer or something similar (or so I read). That’s why I try to copy character by character:

while(i < argc && i < num_states){
    int j = 0;
    while( (states[i][j++] = argv[i][j++]) != '\0' ){
        i++;
    }
    states[i][j] = '\0';
}

Yet it doesn’t work. How should I do this? The compiler gives me this warning when I compile:

$ make ex11
cc -Wall -g    ex11.c   -o ex11
ex11.c: In function ‘main’:
ex11.c:26:28: warning: operation on ‘j’ may be undefined [-Wsequence-point]

I don’t get why it says that j is undefined. valgrind says this:

$ valgrind ./ex11 this is a test
==4539== Memcheck, a memory error detector
==4539== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==4539== Using Valgrind-3.8.0 and LibVEX; rerun with -h for copyright info
==4539== Command: ./ex12 this is a test
==4539== 
arg 4: test
arg 3: a
arg 2: is
arg 1: this
arg 0: ./ex11
state 3, Texas
state 2, Washington
state 1, Oregon
state 0, California
==4539== 
==4539== Process terminating with default action of signal 11 (SIGSEGV)
==4539==  Bad permissions for mapped region at address 0x400720
==4539==    at 0x4005F1: main (ex11.c:26)
==4539== 
==4539== HEAP SUMMARY:
==4539==     in use at exit: 0 bytes in 0 blocks
==4539==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==4539== 
==4539== All heap blocks were freed -- no leaks are possible
==4539== 
==4539== For counts of detected and suppressed errors, rerun with: -v
==4539== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Segmentation fault

[EDIT 1]
A switched my code for this

int i = 0;
while( i < argc && i < num_states ){
    states[i] = argv[i];
    i++;
}

It does work, but the compiler gives me a warning. Also, I realized after posting this question that my previous:

while( (states[i][j++] = argv[i][j++]) != '\0' ){
    i++;
}

Is just plain wrong. Because the j++ is executed twice per loop, and that i++ should be outside that loop in the outer loop. And also as mentioned in the comment below, that the byte-by-byte copy that I try to do using array of arrays doesn’t work because I actually have array of pointers.

That said, is there a way I could do this without having the compiler’s warning?

  • 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-09T21:53:07+00:00Added an answer on June 9, 2026 at 9:53 pm

    One problem (the one that causes the SEGV) is that while states is an array of char *, you’re initializing it with a bunch of string literals (which are const char *). So you should at least be getting a warning on that line. This comes to bite you when you try to write into the string constant (which is in read-only memory).

    Now if you really want to copy the strings from argv to states one byte at a time (rather than just copying pointers — eg states[i] = argv[i]), you need to make sure that states[i][j] is writable. You could do that by making states an array of arrays of chars, instead of an array of pointers to chars:

    char  states[][16] = {
        "California", "Oregon",
        "Washington", "Texas"
    };
    

    Of course, these are fixed size arrays, so you need to worry about overflowing them, but you already had the problem of overflowing the states array itself.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.