Like Tree1Likes
  • 1 Post By grumpy

Problem with array of strings !!

This is a discussion on Problem with array of strings !! within the C Programming forums, part of the General Programming Boards category; Hi, Please excuse me for asking such a trivial question. I am very new to C programming and it would ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    6

    Problem with array of strings !!

    Hi,

    Please excuse me for asking such a trivial question. I am very new to C programming and it would be very helpful if someone answers my question.

    Here is my program ...

    Code:
    #include"scanner.h"
    #include<stdio.h>
    #include"scanner.c"
    #include<stdlib.h>
    #include<string.h>
    
    #define MAX_TOKENS 10
    
    int main(int argc, char **argv){
        int datatype, counter;
        int index = 0;
        char *my_argv[MAX_TOKENS];
        while((datatype = yylex()) != 6)
        {    
            printf("\nData :%s",yytext);
            printf("\nData type :%d\n",datatype);
            my_argv[index++] = yytext;
        }
        printf("\n%s",my_argv[0]);
        printf("\n%s",my_argv[1]);
    }
    Here yytext is of type char*. I store it in my_argv[]. Consider yytex returns "abc" and then "def". I should be getting the output as

    abc
    def

    but instead I get

    abc def
    def

    Can anyone explain me this !!

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,265
    The offending line is
    Code:
        my_argv[index++] = yytext;
    which simply copies a pointer.

    Copying pointers does not copy what they point at.

    1) ensure there is a buffer to be copied to.
    2) copy the required string to that buffer.

    You have completed neither of those steps. A simple way to achieve the first is to make my_argv into a 2-dimensional array (aka an array of arrays), rather than an array of pointers. To copy a string (step 2) look up the function strcpy().

    Also, never .... repeat never .... #include a .c file from another .c file. Simply compile the files separately, and link the objects together.

    Also, the offending line has two side effects (sets a value to my_argv[index] and then increment index). Unless you know exactly what you are doing - and, by your question, you don't - always make each statement have exactly one side effect.
    Code:
        my_argv[index] = yytext;
        ++index;
    (note you still need to fix the first line with this adjustment).
    Last edited by grumpy; 09-04-2012 at 04:29 AM.
    rags_to_riches likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An array of strings problem
    By ineedmunchies in forum C Programming
    Replies: 15
    Last Post: 12-14-2009, 06:48 PM
  2. problem with array of strings
    By cole in forum C++ Programming
    Replies: 11
    Last Post: 09-28-2008, 11:45 PM
  3. array of strings problem
    By dayknight in forum C Programming
    Replies: 3
    Last Post: 11-08-2005, 09:41 AM
  4. problem with array of strings
    By m00se123 in forum C++ Programming
    Replies: 1
    Last Post: 05-24-2002, 12:11 AM
  5. problem with an array of strings in C
    By ornamatica in forum C Programming
    Replies: 14
    Last Post: 05-01-2002, 06:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21