Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C program issue

Status
Not open for further replies.

Hayee

Member level 4
Member level 4
Joined
Mar 4, 2022
Messages
68
Helped
1
Reputation
2
Reaction score
3
Trophy points
8
Activity points
704
Hello guys
I want to do something like this but I think I am not doing it properly.
Will you guys correct me how to do that.

Actually this is a simple routine but it will ease my further routine which will write.

Code:
char A1[] = "12345";
char A2[] = "ABCDE";

char B1[] = "67890";
char B2[] = "FGHIJ";

char C1[] = "13579";
char C2[] = "KLMNO";

unsigned char *Data[6] ={A1,A2,B1,B2,C1,C2};
unsigned int index = 0;

void print_routine(char *ptr)
{
   //printf("index = %u\r\n",index);
   printf("%S\r\n",*ptr);
}

void main()
{
   while(TRUE)
   {
      print_routine(Data[index]);
      delay_ms(1000);
      if(index >= 6) index = 0;
      else index++;
   }
}
 

Hi
I want to do something like this but I think I am not doing it properly.
This is no helpful information. Neither for us, nor for you.

Why don't you tell us
. what you want the software to do
. what`s not working as expected

Klaus
 

My bad
actually what I want to do is if index value is 0, then it should print the value which is in A1 that is 1234 by passing the array to a function.
then this value is incremented to 1, then it should print the value in A2 that is ABCD and so on...
But it is not printing values as I required.

using pointer array (unsigned char *Data with unsinged int index) it will ease to pass value in a function as compare to pass each string like A1,A2,B1 so on....


Hope I am able to deliver my point of view

Thanks
 

Hi,

actually what I want to do is if index value is 0, then it should print the value which is in A1 that is 1234 by passing the array to a function.
then this value is incremented to 1, then it should print the value in A2 that is ABCD and so on...
But "1234" is not the same as 1234.
1234 is an integer number, while "1234" is a string.
You can increment 1234, but you cant increment a string. You can´t increment "1234"

...
BTW: you usually don´t pass an array to a function. You just pass the pointer to the array, or pointer to the array_item.

A1[] is an array of 4+1 bytes.
A2 .. the same

but A1 and A2 don´t form an array, they are independent variables.

Klaus
 

printf("%S\r\n",*ptr);

Here, change this to statement to
Code:
printf("%s\r\n",ptr); /*notice, *ptr --> ptr AND %S --> %s

and it will work.

EXPLANATION:

You are using wrong syntax for %s format specifier:

using %s specifier example:
Code:
#include <stdio.h> 
int main() { 
  char str[] = "exampletext"; 
  printf("%s\n", str); 
  return 0; 
}
Notice that to print exampletext, you need to pass the pointer to first character of the string (str), but not the character (*str) itself.

If you insist that you want to use *ptr itself then use %c format specifier in printf like this:
Code:
  for(int j=0;*ptr!='\0';j++){
       printf("%c",*ptr);
       ptr++;
  }
  printf("\n");
 

Hello!

There are quite a few oddities in your program. The main oddity that prevents
it to work has been reported above by vishweshgm, the printf call is wrong.
But beside this:
- You don't need a global variable for index.
- You are writing a print_routine which does nothing more than printf itself.
-> What's the point?
- As your data consists in strings, there is no point making them unsigned,
it will just bring extra compilation warnings.

I tried this:

Code:
#include <stdio.h>

char A1[] = "12345";
char A2[] = "ABCDE";
char B1[] = "67890";
char B2[] = "FGHIJ";
char C1[] = "13579";
char C2[] = "KLMNO";

char *Data[6] ={A1,A2,B1,B2,C1,C2};

void print_routine(char *ptr) {
   printf("%s\r\n",ptr);
}

void main() {
    char i;
    for(i = 0 ; i < 6 ; ++i) {
        print_routine(Data[i]);
    }
}

Compiles without any error or warning using MacOS. The result is like this:

Code:
Mac-mini:TestC dora$ cc -o TestC TestC.c
Mac-mini:TestC dora$
Mac-mini:TestC dora$ ./TestC
12345
ABCDE
67890
FGHIJ
13579
KLMNO
Mac-mini:TestC dora$

NB: I removed the infinite loop because I don't remember how to get a delay on
MacOS, but basically everything works as long as the printf line is corrected.

@KLAUS:
BTW: you usually don´t pass an array to a function.

Sorry, I'm not really a programming specialist, so I may be wrong on this one, but
if he passes Data to his function, it looks like he's passing a pointer to a
string. Data is an array of 6 pointers to strings. Therefore, any of its elements
is a pointer to string. Is this wrong?

And beside this, I don't understand this:
You can increment 1234, but you cant increment a string. You can´t increment "1234"

Where is he incrementing a sting? As I understand his code, he's just incrementing
a global index.

Dora.
 

Hi Guys
@vishweshgm yes I did it and it is working as I want.

@KlausST
But "1234" is not the same as 1234.
1234 is an integer number, while "1234" is a string.
You can increment 1234, but you cant increment a string. You can´t increment "1234"
Dear Its not a decimal, Its a string.
and I don't want to increment a number but an index number so that I can print next array of string.

My output is like that which I want

1648118503471.png


now in addition what I want to do that is to print the Length of each array.
Each array has a length of 5. I am using the function sizeof to calculate the length of each array

so length should be 5 but I am getting 2. Why it is so
1648118747687.png


Modified code is
Code:
char A1[] = "12345";
char A2[] = "ABCDE";

char B1[] = "67890";
char B2[] = "FGHIJ";

char C1[] = "13579";
char C2[] = "KLMNO";

char *Data[6] ={A1,A2,B1,B2,C1,C2};
unsigned int index = 0;

void print_routine(char *ptr, int length)
{
   printf("%s of length %u\r\n",ptr,length);
}

void main()
{
   while(TRUE)
   {
      print_routine(Data[index],sizeof(Data[index]));
      delay_ms(1000);
      if(index > 5) index = 0;
      else index++;
   }
}

Hope my explanation is well enough for you guys.
 

Where is he incrementing a sting?
I refered to his descripion:
then it should print the value which is in A1 that is 1234 by passing the array to a function.
then this value is incremented to 1
But I guess you are right, that he does not want to increment 'this value' but the index.

Klaus
 

sizeof(Data[index]) gives the size of the pointer. You can e.g. calculate strlen(Data[index]) which gives the actual string length, exclusive terminating 0.
 

I guess you are trying to print the contents of the array Data[] which are strings.

It is better to use 2D-array.

Try this code and let me know if it works are if there are any issues. I have not tested it.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
unsigned char Data[6][6] ={"12345","ABCDE","67890","FGHIJ","13579","KLMNO"};
 
void print_routine(int i, int j) {
    printf("Data[%d][%d] = %s\r\n",i,j,Data[i][j]);  
}
 
void main() {
   while(TRUE) {
      print_routine(4,0);  //prints "13579"
      delay_ms(1000);     
   }
}

 

Data[ i][j] is a character rather than a string pointer, respectively %s isn't correct. You can either print
"%s", Data[ i] or "%s", &Data[ i][j].


Thank you. Corrected it.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
unsigned char Data[6][6] ={"12345","ABCDE","67890","FGHIJ","13579","KLMNO"};
 
void print_routine(int i, int j) {
    printf("Data[%d][%d] = %s\r\n",i,j,Data[i]); 
}
 
void main() {
   while(TRUE) {
      print_routine(4,0);  //prints "13579"
      delay_ms(1000);    
   }
}

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top