#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Unable to get exact output


    #include <stdio.h>
    int main()
    {
    int i,j,k;
    char *str;
    char *names[5][10];

    str = "11001100";
    printf("%s\n",str);
    names[1][10] = str;
    printf("\n%s",names[1][10]);
    for(i=0;i<1;i++)
    {
    for(j=0;j<1;j++)
    {
    if(names[i][j] == '1' && names[i][j]!='\0')
    names[i][j] = '0' ;
    else if(names[i][j] == '0'&& names[i][j]!='\0')
    names[i][j] = '1' ;
    else
    names[i][j] = '\0';
    }
    }
    for(k=0;k<1;k++)
    printf("%s\n",names[k]);

    return 0;
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    2 vital flaws...oops, 3 vital flaws.


    Summary: Don't use uninitialized memory. c index origin is 0. Characters are a bad choice to represent pointers.
    Code:
      char *names[5][10];
      names[1][10] = str;
      printf("\n%s",names[1][10]);
      for(i=0;i<1;i++)
        for(j=0;j<1;j++)
    	names[i][j] = '0' ;
    names can store 5 arrays of 10 arrays of pointers to character. Without playing clever memory tricks you'd normally index these as
    names[i] with i taking any of the integral values 0 through 4,
    and
    names[i][j] with j being any of the integers 0 through 9.

    names[1][10] = str; can therefor be regarded as a clever memory trick or a blunder. It's a blunder.

    Your loops can be replaced by
    i = j = 0;
    which might not be your final intent but simplifies the discussion.

    names[1][10], where ever that may be, is guaranteed by the c standard to not be names[0][0]. names[1][10] is the only part of names you initialized.

    In the rest of the code you've treated names as if it were an array of an array of char.


    Every time I look I find a new troubling feature, and begin to wonder if your post simply belongs in the jokes thread.


    else if(names[i][j] == '0'&& names[i][j]!='\0')
    if names[i][j] is '0' then it's certainly not ASCII nul .
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    As a newcomer, you need to be aware that the forum software does not by default retain indentation. Therefore, you need to put code samples in between [code] tags, like so:

    [code]
    code goes here.
    [/code]

    You can do this automatically with the '#' button at the top of the editing window, or the highlight marker button right next to it. As a courtesy to you, I have taken the liberty of putting your code in [highlight] tags for you:
    C Code:
    #include <stdio.h>
     
    int main()
    {
        int i,j,k;
        char *str;
        char *names[5][10];
     
        str = "11001100";
        printf("%s\n",str);
        names[1][10] = str;
        printf("\n%s",names[1][10]);
        for(i=0; i<1; i++)
        {
            for(j=0; j<1; j++)
            {
                if(names[i][j] == '1' && names[i][j]!='\0')
                    names[i][j] = '0';
                else if(names[i][j] == '0'&& names[i][j]!='\0')
                    names[i][j] = '1';
                else
                    names[i][j] = '\0';
            }
        }
        for(k=0; k<1; k++)
            printf("%s\n",names[k]);
     
        return 0;
    }


    Now that we can read the code more easily, can you tell us just what isn't working for you? Just saying "it doesn't work" isn't enough information for us to go by; we need to know how it is failing. What output are you getting, and what were you expecting?
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,143
    Rep Power
    2222
    You get exactly the exact output that you tell the program to give you.

    Now, if you were expecting output that is different from what you're telling the program to give you, then there are two very necessary pieces of information that you have failed to provide us. Without that information, we cannot possibly help you. That information is:

    1. What output you expected.

    2. What output you got instead.


    Now, my next question is: Why are you trying to run a program that is broken?

    You should only try to run a program that has compiled cleanly. A program that has compiled cleanly has no errors nor warnings. Here is what I got when I compiled your program (using MinGW gcc with all warnings turned on):
    C:\otros\dcw>gcc -Wall badam.c
    badam.c: In function `main':
    badam.c:17: warning: comparison between pointer and integer
    badam.c:18: warning: assignment makes pointer from integer without a cast
    badam.c:19: warning: comparison between pointer and integer
    badam.c:20: warning: assignment makes pointer from integer without a cast
    badam.c:26: warning: char format, pointer arg (arg 2)

    C:\otros\dcw>
    Those are very serious problems that the compiler is warning you about. Why are you ignoring warnings? With problems like those, your program is almost guaranteed to do things that you do no expect. I've been programming professionally for 30 years and in C and C++ for over 20 years and even I cannot predict what your broken program might end up doing.

    Now, when you assign strings, eg:
    names[1][10] = str;
    do you intend to store that strings memory address at that location? Or do you intend to copy the string to that location? If you intend to copy the string, then use strcpy(). You should also review C-style strings.

    Also, you declare names thus:
    char *names[5][10];
    That means that every element is a character pointer, so you cannot assign characters to any element. If you intended to declare an array of five strings, each of which can contain a string that is 9 characters long (if you don't understand that, then review C-style strings), then it should be this:
    char names[5][10];

    And also, please use code tags when you post code here:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i,j,k;
        char *str;
        char *names[5][10];
    
        str = "11001100";
        printf("%s\n",str);
        names[1][10] = str;
        printf("\n%s",names[1][10]);
        for(i=0;i<1;i++)
        {
            for(j=0;j<1;j++)
            {
                if(names[i][j] == '1' && names[i][j]!='\0')
                    names[i][j] = '0' ;
                else if(names[i][j] == '0'&& names[i][j]!='\0')
                    names[i][j] = '1' ;
                else
                    names[i][j] = '\0';
            }
        }
        for(k=0;k<1;k++)
            printf("%s\n",names[k]);
    
        return 0;
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by Schol-R-LEA
    As a newcomer, you need to be aware that the forum software does not by default retain indentation. Therefore, you need to put code samples in between [code] tags, like so:

    [code]
    code goes here.
    [/code]

    You can do this automatically with the '#' button at the top of the editing window, or the highlight marker button right next to it. As a courtesy to you, I have taken the liberty of putting your code in [highlight] tags for you:
    C Code:
    #include <stdio.h>
     
    int main()
    {
        int i,j,k;
        char *str;
        char *names[5][10];
     
        str = "11001100";
        printf("%s\n",str);
        names[1][10] = str;
        printf("\n%s",names[1][10]);
        for(i=0; i<1; i++)
        {
            for(j=0; j<1; j++)
            {
                if(names[i][j] == '1' && names[i][j]!='\0')
                    names[i][j] = '0';
                else if(names[i][j] == '0'&& names[i][j]!='\0')
                    names[i][j] = '1';
                else
                    names[i][j] = '\0';
            }
        }
        for(k=0; k<1; k++)
            printf("%s\n",names[k]);
     
        return 0;
    }


    Now that we can read the code more easily, can you tell us just what isn't working for you? Just saying "it doesn't work" isn't enough information for us to go by; we need to know how it is failing. What output are you getting, and what were you expecting?

    my intention is I want to replace the array values '0' by '1' and '1' by '0', I gave one input ie "11001100" but I want output like "00110011" that's why iam trying in this way , in compiling time getting some warning ie
    1) comparison between pointer and integer
    2) assignment makes pointer from integer without cast

    please check and send me the valuable suggestions

    thanks in advance
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Hey Badam,

    you need to make quite few corrections,

    add #include<string.h> // so that you can use inbuilt function

    then instead of statement names[1][10] = str; use strcpy(names[0],str)

    here I have corrected your code


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main(){
     	int i,j,k;
     	char *str="11001100";
     	char names[5][10];
    	strcpy(names[0],str);
        //str = "";
        //printf("%s\n",str);
        //names[1][10] = str;
        printf("%s\n",names[0]);
       for(i=0; i<1; i++)
        {
            for(j=0; j<10; j++) // need to loop for 10 times names[][10] is 10 times here 
            {
                if(names[i][j] == '1' && names[i][j]!='\0')
                    names[i][j] = '0';
                else if(names[i][j] == '0'&& names[i][j]!='\0')
                    names[i][j] = '1';
                else
                    names[i][j] = '\0';
            }
        }
        for(k=0; k<1; k++)
            printf("%s\n",names[k]); 
     	getch();
    }
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by raadys
    Hey Badam,

    you need to make quite few corrections,

    add #include<string.h> // so that you can use inbuilt function

    then instead of statement names[1][10] = str; use strcpy(names[0],str)

    here I have corrected your code


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main(){
     	int i,j,k;
     	char *str="11001100";
     	char names[5][10];
    	strcpy(names[0],str);
        //str = "";
        //printf("%s\n",str);
        //names[1][10] = str;
        printf("%s\n",names[0]);
       for(i=0; i<1; i++)
        {
            for(j=0; j<10; j++) // need to loop for 10 times names[][10] is 10 times here 
            {
                if(names[i][j] == '1' && names[i][j]!='\0')
                    names[i][j] = '0';
                else if(names[i][j] == '0'&& names[i][j]!='\0')
                    names[i][j] = '1';
                else
                    names[i][j] = '\0';
            }
        }
        for(k=0; k<1; k++)
            printf("%s\n",names[k]); 
     	getch();
    }


    its working very nice ,thank you very much

IMN logo majestic logo threadwatch logo seochat tools logo