Thread: Printing problem when I use NULL

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    10

    Printing problem when I use NULL

    I initialize array with characters a2[3][4] from a1 array and I want to insert a NULL ‘\0’ to the items a2[i][4] in the array and then when I Print the array I don’t get all characters printed and I don’t know why, and when I don’t store ‘\0’ it prints ok.

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    
        int a1[3][3];
        char a2[3][4];
        char a3[10]= {'*','#','@','%','&','!','^','>','<',':'};
    
        int i,j;
    
         a1[0][0]=6;        /* storing values in the array a1 */
         a1[0][1]=3;
         a1[0][2]=0;
    
         a1[1][0]=4;
         a1[1][1]=9;
         a1[1][2]=2;
    
         a1[2][0]=2;
         a1[2][1]=6;
         a1[2][2]=7;
    
    
                
             for (i=0;i<=2;i++)
             {
                for (j=0;j<=2;j++)
                {
                    a2[i][j]= a3[a1[i][j]]; /* storing characters in the array a2 */
    
    
                    if (j = 2)
                        a2[i][j+1]='\0';  /* storing null in a2[i][3] */
                                          /* ****** the problem in the above line */
                }
              }
             printf("\n");
    
             for (i=0;i<=2;i++)
             {
                for (j=0;j<=3;j++)
                {
                     printf("%c",a2[i][j]); /* printing character from array a2 */
                                       }
                }
    
    
    	return(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > if (j = 2)
    If your compiler isn't warning you about = when you should be using ==, then turn up the warning level
    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.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    You are right it was off, I turned it on,
    and the problem is solved .. many thanks.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want to insert a NULL ‘\0’
    Just so that you're clear on this, NULL and '\0' are not equivalent. '\0' is the nul character, NULL is a macro representing a null pointer.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  5. Bitmap Displaying Problem
    By MadCow257 in forum Game Programming
    Replies: 0
    Last Post: 01-25-2005, 05:21 PM