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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:04:37+00:00 2026-05-30T08:04:37+00:00

below is a small C application. It will ask you for a word to

  • 0

below is a small C application. It will ask you for a word to input. It stops asking when has attained four unique words. But in the form shown below it won’t run properly until you uncomment the relevant lines.

#include <stdio.h>
#include <string.h>

#define WORDS_COUNT 4

int main()
{
    char* words[WORDS_COUNT];

    int words_added = 0;
    while (words_added<WORDS_COUNT)
    {

        puts ("\n-------enter a word-------");

        char response[250];

        scanf("%s", response);

        int i;
        int duplicate_flag = 0;
        for (i=0; i < words_added; i++)
        {
            if (strcmp(words[i], response) == 0)
            {
                duplicate_flag = 1;
                break;
            };
        };

        if (duplicate_flag == 0)
        {
            //char tmp[250];
            //strcpy(tmp, response);
            words[words_added] = response; //words[words_added] = tmp;
            puts("that's new!");
            words_added ++;
        } else {
            puts("you've said that already...");
        };

    };
    return 0;
};

The major difference as you can see is between words[words_added] = response and words[words_added] = tmp.

Why would the tmp variable work and not the response?

I’m guessing that response will have the exact same address every iteration, and tmp will get a new address every iteration. but why? yet they were both declared in same the while loop???

  • 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-30T08:04:38+00:00Added an answer on May 30, 2026 at 8:04 am

    When you assign words[words_added] = response you’re copying the address (not the contents) of response into the array. So in the original form, your code should see the second word and every subsequent word as duplicates. When you use tmp the code compares each new response to the previous tmp that was stored in (every location of) words[], then copies it into tmp if it’s not a duplicate.

    So I suspect that your code will detect a duplicate that immediately follows the original, but not one that occurs 2 or more words later.

    The words array contains 4 pointers, but no memory has been allocated to those pointers.
    You need to allocate memory for each element of the words array, and then copy each string into it:

    if (duplicate_flag == 0)
    {
      words[words_added++] = strdup(response); // allocates mem and copies the string
      puts("that's new!");
    } else {
      ...
    }
    

    Then be sure to free the memory at the end of your program:

    for (i = 0; i < words_added; ++i) {
      free(words[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've put together a small code-sample below (Currently in C# 3.5 but would also
I've done a small experiment as will be shown below and it looks like
I thought of solution below because the collection is very very small. But what
in my application there is a small part of function,in which it will read
I have written a small application which will be used in my work environment
I have a small script which im using to test PHP mail(), as below:
I have developed a small application. I created a login panel for it. I
I'm trying to figure out how to design a small application more elegantly, and
I am new to iphone development.. am creating one small application where two viewcontrollers
I have a domain that will be accessed by a small, private group of

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.