指向数组的指针数组。在 PIC18 MPLAB IDE 中打印字符串

发布于 2024-12-13 14:33:21 字数 1106 浏览 2 评论 0原文

我正在使用 MPLAB IDE 编写在 PIC 18 微控制器中使用的 C 代码。 我有 5 个字符串,我希望能够使用指针。 这个想法是拥有一个包含指向字符串数组的指针的数组。并将它们打印在控制台上。 下面的代码编译时没有错误或警告,但我在控制台上得到的都是垃圾。

有人能指出我正确的方向吗?非常感谢。 抱歉,如果我的代码格式不正确。

#include <stdio.h>
#include <p18f4520.h>
#include <stdlib.h> 
#pragma config WDT = OFF

#define size 64

#pragma romdata s1=0x300                        //specific ROM addresses for the data
  rom char *s1[] = "Hello";
#pragma romdata s2 = 0x307
  rom char *s2 = "Welcome to C programming";

#pragma romdata s3=0x31A
  rom char *s3= "My name is ";  

#pragma romdata s4=0x32C
  rom char *s4 = "Pic18 program";

#pragma romdata s5=0x33A
  rom char *s5 ="Goodbye, I hope this works!";


void printString(const char*);

void main (void) 
{   
    int i=0;

    char stringArray [] = {*s1, *s2, *s3, *s4, *s5};
    char *ptr=stringArray;


       while(i<5)
        {
             printString(&ptr[i]);
             i++;
          }
}

void printString( const char *strPtr)
{
   while(*strPtr !='\0')
     {
       printf("%c", strPtr);
       strPtr++;

      } 
}

`

I am writing a C-code to use in PIC 18 micro-controller using MPLAB IDE.
I have 5 strings and I want to be able to use pointers.
The idea is having an array containing the pointers to the string arrays. and have them print on the console.
The code below compiles with No errors or warnings, but all I get on the console is garbage.

Can someone point me to the right direction. many thanks.
sorry if my formatting of the code is not right.

#include <stdio.h>
#include <p18f4520.h>
#include <stdlib.h> 
#pragma config WDT = OFF

#define size 64

#pragma romdata s1=0x300                        //specific ROM addresses for the data
  rom char *s1[] = "Hello";
#pragma romdata s2 = 0x307
  rom char *s2 = "Welcome to C programming";

#pragma romdata s3=0x31A
  rom char *s3= "My name is ";  

#pragma romdata s4=0x32C
  rom char *s4 = "Pic18 program";

#pragma romdata s5=0x33A
  rom char *s5 ="Goodbye, I hope this works!";


void printString(const char*);

void main (void) 
{   
    int i=0;

    char stringArray [] = {*s1, *s2, *s3, *s4, *s5};
    char *ptr=stringArray;


       while(i<5)
        {
             printString(&ptr[i]);
             i++;
          }
}

void printString( const char *strPtr)
{
   while(*strPtr !='\0')
     {
       printf("%c", strPtr);
       strPtr++;

      } 
}

`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哭泣的笑容 2024-12-20 14:33:21

嗯,这就是答案。

#include <stdio.h>
#include <p18f4520.h>
#include <stdlib.h> 
#include <string.h>
#pragma config WDT = OFF

#pragma romdata s1=0x300                      //specific ROM addresses for the data
 rom char s1[] = "Hello";
#pragma romdata s2 =0x307
   rom char s2[] = "Welcome to C programming";
#pragma romdata s3=0x41A
   rom char s3[] = "My name is";
#pragma romdata s4=0x32C
  rom char s4[] = "Pic18 program";
#pragma romdata s5=0x33A
   rom char s5[] ="Goodbye, I hope this works!";

void printAndCount(rom char *strPtr);
int j=0; int i=0;
void main (void)
{
    rom char* stringArray [] = {s1, s2, s3, s4, s5};
                    while(i<5 )
                {
                    printAndCount(stringArray[i]);  
                            i++;
                }
}
void printAndCount(rom char *strPtr)
    {   
    while (*strPtr != '\0' )
        {   printf("%c", *strPtr);
            strPtr++;
            ++j;    
                }
            printf("     %d\n",j);
            j=0;
}

Well This is the answer.

#include <stdio.h>
#include <p18f4520.h>
#include <stdlib.h> 
#include <string.h>
#pragma config WDT = OFF

#pragma romdata s1=0x300                      //specific ROM addresses for the data
 rom char s1[] = "Hello";
#pragma romdata s2 =0x307
   rom char s2[] = "Welcome to C programming";
#pragma romdata s3=0x41A
   rom char s3[] = "My name is";
#pragma romdata s4=0x32C
  rom char s4[] = "Pic18 program";
#pragma romdata s5=0x33A
   rom char s5[] ="Goodbye, I hope this works!";

void printAndCount(rom char *strPtr);
int j=0; int i=0;
void main (void)
{
    rom char* stringArray [] = {s1, s2, s3, s4, s5};
                    while(i<5 )
                {
                    printAndCount(stringArray[i]);  
                            i++;
                }
}
void printAndCount(rom char *strPtr)
    {   
    while (*strPtr != '\0' )
        {   printf("%c", *strPtr);
            strPtr++;
            ++j;    
                }
            printf("     %d\n",j);
            j=0;
}
可爱咩 2024-12-20 14:33:21

问题是在您的代码中, stringArray 代表存储每个字符串的第一个字符的内存块,而不是它们的地址,请尝试以下操作:

void main (void) 
{   
    int i=0;

    char * stringArray [] = {s1, s2, s3, s4, s5};
    char **ptr=stringArray;


       while(i<5)
        {
             printString(*ptr[i]);
             i++;
          }
}

The problem is that in your code, stringArray stands for a memory block that stores the first character of each string, not the address of them, try this:

void main (void) 
{   
    int i=0;

    char * stringArray [] = {s1, s2, s3, s4, s5};
    char **ptr=stringArray;


       while(i<5)
        {
             printString(*ptr[i]);
             i++;
          }
}
二货你真萌 2024-12-20 14:33:21

该代码中有几个基本错误。就用这个吧。

#include <stdio.h>
#include <p18f4520.h>
#include <stdlib.h> 
#pragma config WDT = OFF

#define size 64

// store ARRAYS here not pointers!
// Im not sure what non-standard "rom" means more exactly, 
// but these should be const in the C language
#pragma romdata s1=0x300                        //specific ROM addresses for the data
  rom const char s1[] = "Hello";                      
#pragma romdata s2 = 0x307
  rom const char s2[] = "Welcome to C programming";   

#pragma romdata s3=0x31A
  rom const char s3[] = "My name is ";                

#pragma romdata s4=0x32C
  rom const char s4[] = "Pic18 program";

#pragma romdata s5=0x33A
  rom const char s5[] ="Goodbye, I hope this works!";


void printString (const char* strPtr);

void main (void) 
{   
    int i=0;

    // this, however, must be an array of pointers (that point to the strings)
    // since the strings are const, the pointers must be declared pointer-to-const:
    const char* stringArray [] = {s1, s2, s3, s4, s5};

    while(i<5)
    {
         printString(stringArray[i]);  // now this syntax suddenly turned sane
         i++;
    }
}

void printString (const char *strPtr)
{
   while(*strPtr !='\0')
     {
       printf("%c", *strPtr);
       strPtr++;
      } 
}

There are several fundamental errors in that code. Use this one instead.

#include <stdio.h>
#include <p18f4520.h>
#include <stdlib.h> 
#pragma config WDT = OFF

#define size 64

// store ARRAYS here not pointers!
// Im not sure what non-standard "rom" means more exactly, 
// but these should be const in the C language
#pragma romdata s1=0x300                        //specific ROM addresses for the data
  rom const char s1[] = "Hello";                      
#pragma romdata s2 = 0x307
  rom const char s2[] = "Welcome to C programming";   

#pragma romdata s3=0x31A
  rom const char s3[] = "My name is ";                

#pragma romdata s4=0x32C
  rom const char s4[] = "Pic18 program";

#pragma romdata s5=0x33A
  rom const char s5[] ="Goodbye, I hope this works!";


void printString (const char* strPtr);

void main (void) 
{   
    int i=0;

    // this, however, must be an array of pointers (that point to the strings)
    // since the strings are const, the pointers must be declared pointer-to-const:
    const char* stringArray [] = {s1, s2, s3, s4, s5};

    while(i<5)
    {
         printString(stringArray[i]);  // now this syntax suddenly turned sane
         i++;
    }
}

void printString (const char *strPtr)
{
   while(*strPtr !='\0')
     {
       printf("%c", *strPtr);
       strPtr++;
      } 
}
留蓝 2024-12-20 14:33:21

试试这个:

#include <stdio.h>
#include <p18f4520.h>
#include <stdlib.h> 
#pragma config WDT = OFF

#pragma romdata s1=0x300                        //specific ROM addresses for the data
  rom char s1[] = "Hello";
#pragma romdata s2 = 0x307
  rom char s2[] = "Welcome to C programming";
#pragma romdata s3=0x31A
  rom char s3[] = "My name is ";  
#pragma romdata s4=0x32C
  rom char s4[] = "Pic18 program";
#pragma romdata s5=0x33A
  rom char s5[] ="Goodbye, I hope this works!";


void main (void) 
{   
    int i;
    char *stringArray [] = {s1, s2, s3, s4, s5};

    for (i = 0;  i < 5;  ++i)
    {
        printf("%s", stringArray[i]);
    }
}

它简化并进行了多项修复,修复问题最重要的是消除了 stringArray 中每个字符串名称之前的 *。其他更改是按惯例的 C 编码进行的。

Try this instead:

#include <stdio.h>
#include <p18f4520.h>
#include <stdlib.h> 
#pragma config WDT = OFF

#pragma romdata s1=0x300                        //specific ROM addresses for the data
  rom char s1[] = "Hello";
#pragma romdata s2 = 0x307
  rom char s2[] = "Welcome to C programming";
#pragma romdata s3=0x31A
  rom char s3[] = "My name is ";  
#pragma romdata s4=0x32C
  rom char s4[] = "Pic18 program";
#pragma romdata s5=0x33A
  rom char s5[] ="Goodbye, I hope this works!";


void main (void) 
{   
    int i;
    char *stringArray [] = {s1, s2, s3, s4, s5};

    for (i = 0;  i < 5;  ++i)
    {
        printf("%s", stringArray[i]);
    }
}

It simplifies and makes several fixes, the most significant to fix the problem was eliminating the * before each string name in stringArray. Other changes are made to be customary C coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文