for loop problem driving me insane!!

This is a discussion on for loop problem driving me insane!! within the C Programming forums, part of the General Programming Boards category; well, it just does not make any sense... here is my code Code: #include<stdio.h> int plain[3],key[3][3],cip[3]; int conv_to_num(char p) { ...

  1. #1
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136

    for loop problem driving me insane!!

    well,

    it just does not make any sense...

    here is my code

    Code:
    #include<stdio.h>
    
    int plain[3],key[3][3],cip[3];
    
    int conv_to_num(char p)
    {
        int val=0;
        val=(int) p;
        printf("p=%c,val=%d\n",p,val);
        val=val-97;
        printf("val=%d\n",val);
        return val;
    }
    
    void enc()
    {
        char key1[9],plain1[3],cip1[3];
        int temp[9],i=0,j=0,k=0;
        k=0;
        printf("Enter the key matrix in the form of a string==");
       gets(key1);
        printf("Enter the plain text matrix in the form of a string==");
        gets(plain1);
        for(i=0;i<9;i++)
        {
            printf("key[%d]=%c\n",i,key1[i]);
            temp[i]=conv_to_num(key1[i]);
            printf("i=%d,%d\n",i,temp[i]);
        }
    
         for(i=0;i<3;i++)
        {
            plain[i]=conv_to_num(plain1[i]);    
            printf("%d\n",plain[i]);    
        }
    }
    
    int main()
    {
         enc();
         return 0;
    }
    this the output i get...

    Code:
    $ gcc ........edup.c 
    /tmp/ccMKBfee.o: In function `enc':
    ........edup.c:(.text+0x87): warning: the `gets' function is dangerous and should not be used.
    $ ./a.out
    Enter the key matrix in the form of a string==asdfghjkl
    Enter the plain text matrix in the form of a string==asd
    key[0]=
    p=,val=0
    val=-97
    i=0,-97 ****what the????****
    key[1]=s
    p=s,val=115
    val=18
    i=1,18
    key[2]=d
    p=d,val=100
    val=3
    i=2,3
    key[3]=f
    p=f,val=102
    val=5
    i=3,5
    key[4]=g
    p=g,val=103
    val=6
    i=4,6
    key[5]=h
    p=h,val=104
    val=7
    i=5,7
    key[6]=j
    p=j,val=106
    val=9
    i=6,9
    key[7]=k
    p=k,val=107
    val=10
    i=7,10
    key[8]=l
    p=l,val=108
    val=11
    i=8,11
    p=a,val=97
    val=0
    0
    p=s,val=115
    val=18
    18
    p=d,val=100
    val=3
    3   ****Surprisingly this came out nicely*****
    the problem is that the first character of the string key1 is not passed to the function "conv_to_num()", but characters of string plain1 get processed by the function correctly? how and why is this happening to me???


    i m using gcc --version
    gcc (GCC) 4.3.2 20081105 (Red Hat 4.3.2-7)

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    500
    I get the following output.
    Enter the key matrix in the form of a string==asdfghjkl
    Enter the plain text matrix in the form of a string==asd
    key[0]=a
    p=a,val=97
    val=0
    i=0,0
    key[1]=s
    p=s,val=115
    val=18
    i=1,18
    key[2]=d
    p=d,val=100
    val=3
    i=2,3
    key[3]=f
    p=f,val=102
    val=5
    i=3,5
    key[4]=g
    p=g,val=103
    val=6
    i=4,6
    key[5]=h
    p=h,val=104
    val=7
    i=5,7
    key[6]=j
    p=j,val=106
    val=9
    i=6,9
    key[7]=k
    p=k,val=107
    val=10
    i=7,10
    key[8]=l
    p=l,val=108
    val=11
    i=8,11
    p=a,val=97
    val=0
    0
    p=s,val=115
    val=18
    18
    p=d,val=100
    val=3
    3
    Is the above the expected output?
    Edit:
    I am using Mingw port of gcc on Windows XP
    Last edited by stevesmithx; 12-21-2008 at 11:53 AM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    So you're using gets(), it tells you not to, and NOW you're confused by the buffer overflow ???
    9 chars doesn't fit into an array of 9 chars, whilst still allowing room for a \0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Explaining a bit more.
    gets means get string. A string in C is terminated always by a \0. All the string functions require that otherwise you get undefined behaviour.
    So if you have asd the string will be 'a' 's' 'd' '\0'.

    More in detail, gets stops when it finds a white space. Usually ' ', '\t' or '\n'. In your case the newline '\n'. Then it stores everything, PLUS a '\0' in the buffer. So in the case of key1 it will store 9 chars and '\0' in a key1, total 10, which only holds 9 chars. Bad, bad, bad

  5. #5
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    500
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    Man,I love that signature.
    Holds true all the times in programming
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Thanks
    Creeping Death is presently hobbling around the emergency wards, wondering where all this blood is coming from
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    500
    Rofl
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  8. #8
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    lol...it was quite stupid of me to overlook the array size...ran the program on gdm and found out the problem...thanks a lot people...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. This problem is driving me nuts!(function call)
    By System_159 in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2006, 06:48 PM

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