Thread: Array of Const Char

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    14

    Array of Const Char

    Hello, I have been trying to generate array of Const Char but I couldnt make it work. In my program, I wanna do the following:

    Ask the user the number of inputs (channels)
    Then save each input into an array of Const Char
    Also the program will add a string in front of each input

    For example:
    enter the number of inputs: 3
    enter input 1 : 0
    enter input 2 : 12
    enter input 3 : 3

    Code:
    const char physicalChannel[Number of Inputs][2]
    from these inputs I want to obtain:
    physicalChannel[0]="Dev/ai0"
    physicalChannel[1]="Dev/ai12"
    physicalChannel[2]="Dev/ai3"

    I did the following but it didnt work:

    #include <stdio.h>

    Code:
    int main(void)
    {
      int d;
      int ChnCount =3;
      const char physicalChannel[ChnCount][2];  
      
    	for (d=0; d<ChnCount; d++)
      	{
    	printf(" Enter the ID no. (0 to 7) for channel %d of %d: ",d+1, ChnCount);   /*------------*/
    	scanf("Dev/ai%s", &physicalChannel[d]); 
    	}
    
    
      for(d = 0;d < ChnCount;++d)
      {
    	printf ("%s \n", physicalChannel[d]);
      }
     
      return 0;
    }
    Can you please help me with this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It can't be const if you intend to perform a run-time assignment to it.

    Also [2] needs to be [3] if you want to store a 2-character string in it.
    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
    Jun 2017
    Posts
    14
    Okay, I implemented the changes you suggested. It still doesnt work. Here is the code.

    Code:
    #include <stdio.h>
    #include <string.h>
     
     
    int main(void)
    {
      int d;
      int ChnCount =2;
      char physicalChannel[ChnCount][12];
    
    
        for (d=0; d<ChnCount; d++)
          {
        printf(" Enter the ID no. (0 to 39) for channel %d of %d: ",d+1, ChnCount);   /*------------*/
        scanf("Dev/ai%d", &physicalChannel[d]);  
        }
    
    
      for(d = 0;d < ChnCount;++d)
      {
        printf ("%s \n", physicalChannel[d]);
      }
     
      return 0;
    }
    In this case, the code asks me the first input. Whenever I enter the first input, it instantly prints without asking for the second input. Output is also incorrect. Below is my output:

    Code:
     Enter the ID no. (0 to 39) for channel 1 of 2: 1
     Enter the ID no. (0 to 39) for channel 2 of 2: ý
    ´
    Last edited by cyln; 10-10-2017 at 06:39 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf("Dev/ai%d", &physicalChannel
    1. Your array is the wrong type.
    2. Unless you type in Dev/ai7, it isn't going to work. In scanf, every letter matches itself apart 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.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    you might want to work with loops situated like this instead. This is is a work in progress.
    something you can tinker with to see how this works.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
     
        int d,b;
        int ChnCount =2;
        char physicalChannel[ChnCount][12];
        char bigstuff[30];
        int count = 0;
        int wantCount = 0;
        
    //     printf(" Enter the ID no. (0 to 39) for channel %d of %d: ",d+1, ChnCount);   /*------------*/
    
        printf("enter amount here --> ");
        scanf("%d",&wantCount);
        while ( count <= wantCount)
        {
            
                
                
            if ( count == wantCount )
            {
                for ( b = 0; b < wantCount; b++)
                {
                    printf("%c\n", bigstuff[b]);
                
                }
            }
            
            printf("now enter something else here -->");
                
                scanf("%s",&bigstuff[d++]);
                count++;
                printf("count %d wantCount %d\n", count, wantCount);
                
                
    }
    return 0;
    }
    you still have to figure out how to take in more then one char then to print it out properly as stated in a post above this one.
    Code:
    bigstuff [element number plus size is ? goes in here ] = 1 (digit)
    bigstuff [element number plus size is ? goes in here ] = 2 (digits)
    they are going to fluctuate. So you'll have to devise a means to deal with that.
    Last edited by userxbw; 10-10-2017 at 08:24 AM.

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    14
    Thanks a lot for your replies. The above code did not work as well. What I want is quite simple but I cannot sort it out for some reason.

    The short code I write here is a part of another code. The original data acquisition code requires use of a "for loop". Therefore, I have to input arrays of string through ''for'' loop.
    Also, physicalChannel is defined as ''const char''

    I wanna define the following via a user input.

    physicalChannel[0]="Dev/ai0"
    physicalChannel[1]="Dev/ai12"
    physicalChannel[2]="Dev/ai3"

    Can anyone tell/show me how to do this please?

    thanks

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    14
    Hi again, the following works. But I have to write the whole string i.e.: I type "Dev/ai22". Instead, I only want to type 22 and obtain Dev/ai22. Any solution to this please?

    Code:
    #include <stdio.h>#include <string.h>
    
    
    int main(void)
    {
      int d;
      int ChnCount =2;
       const char physicalChannel[ChnCount][12];
    
    
      
    	for (d=0; d<ChnCount; d++)
      	{
    	printf(" Enter the ID no. (0 to 39) for channel %d of %d, Add 'Dev1/ai' in front of each ID: ",d+1, ChnCount);   /*------------*/
    	scanf("%s", &physicalChannel[d]); 
    	}
    
    
      for(d = 0;d < ChnCount;++d)
      {
    	printf ("%s \n", physicalChannel[d]);
      }
     
      return 0;
    }

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Oh ok, sorry.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
    char woohoo[30];
    char addme[40] = " Dev/ai";
    
    printf("gimme something yo!\n");
    scanf("%s", &woohoo);
    strcat(addme, woohoo);
    
    printf("cat =  %s\n",addme);
    return 0;
    }
    results
    Code:
    [userx@void bin]$ ./a.out
    gimme something yo!
    33
    cat =   Dev/ai33
    or just add that little bit "Dev/ai" to your prinf output
    Last edited by userxbw; 10-10-2017 at 09:52 AM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Okay you say you want the user to only enter the numeric suffix so why not something like:

    Code:
    #include <stdio.h>
    #include <string.h>
     
     
    int main(void)
    {
        int d;
        const int ChnCount = 2;  // Note the const to avoid VLA issues.
        char physicalChannel[ChnCount][12];
        int number;
    
        for (d = 0; d < ChnCount; d++)
        {
            printf(" Enter the ID no. (0 to 39) for channel %d of %d: ",d+1, ChnCount);   /*------------*/
            scanf("%d", &number);
            sprintf(physicalChannel[d], "Dev1/a%d", number); 
        }
     
     
      for(d = 0; d < ChnCount; ++d)
      {
        printf ("%s \n", physicalChannel[d]);
      }
      
      return 0;
    }

    You may want to validate the number before you insert it into your string to insure it is a valid channel.

    Jim

  10. #10
    Registered User
    Join Date
    Jun 2017
    Posts
    14
    Thanks a lot for your replies. The last code gives my my desired output. I have one question remaining.

    I am programming a data acquisition device in C. So my code includes the library of this device as well. In reference help of this device tells me that:

    Code:
    int32 DAQmxCreateAIVoltageChan (TaskHandle taskHandle, const char physicalChannel[], const char nameToAssignToChannel[], int32 terminalConfig, float64 minVal, float64 maxVal, int32 units, const char customScaleName[]);
    so physicalChannel is defined as "const char". Does it really matter if I define physicalChannel as "char" just like you defined above?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by cyln View Post
    ... so physicalChannel is defined as "const char". Does it really matter if I define physicalChannel as "char" just like you defined above?
    This is perfectly fine. The const parameter means that the function should not be modifying the data pointed to. The type in the caller does not need to be const.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare unsigned char array with const char*
    By Lazar in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2015, 08:36 AM
  2. using fopen with const char array?
    By traxy in forum C Programming
    Replies: 9
    Last Post: 09-16-2008, 02:53 PM
  3. const char* to char array.
    By kevinawad in forum C++ Programming
    Replies: 13
    Last Post: 08-05-2008, 02:25 PM
  4. Converting const char to char array
    By HomerJ in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 03:21 PM
  5. How do I cast a char Array[] to a const char* ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2001, 08:43 AM

Tags for this Thread