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.

error with pointer as string

Status
Not open for further replies.

kahroba92

Junior Member level 1
Joined
Jul 7, 2016
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
138
Hi freinds,

I want to write five 'M' in a string. I have defined string by char * as following code:

Code:
#include [COLOR="#000000"]<[/COLOR]stdio.h>
#include [COLOR="#000000"]<[/COLOR]stdlib.h>

int main()
{
    char *str;
    int i=0;
    while(i<5)
    {
        *(str+i) = 'M';
         i++;
    }

    *(str+5) = '\0';

    printf("%s", str);


    return 0;
}

if I write "char str[5]" instead of "char *str" my code will execute well. I can't understand why this pointer to string doesn't work.
what's my problem? can anybody help?
tnx
 
Last edited by a moderator:

"char str[5]" reserves 5 bytes of memory for the string.
The first is str[0] = *(str + 0) and the last is str[4] = *(str + 4).
You write to *(str + 5) which is outside the array. It it pure luck that it works.
"str" is not a pointer, it is the start address of the array. "str" behaves almost like a pointer, but you can't assign a value to it.

"char *str" only defines only the pointer, no space for the characters.
A function local variable of type "auto" (default) has random/unknown data before a value is assigned.
When you do *(str + i) = 'M' you write to a random address.

You can do this to make the pointer version to work:
char my_string[6];
char *str = my_string;
 
"char str[5]" reserves 5 bytes of memory for the string.
The first is str[0] = *(str + 0) and the last is str[4] = *(str + 4).
You write to *(str + 5) which is outside the array. It it pure luck that it works.
"str" is not a pointer, it is the start address of the array. "str" behaves almost like a pointer, but you can't assign a value to it.

"char *str" only defines only the pointer, no space for the characters.
A function local variable of type "auto" (default) has random/unknown data before a value is assigned.
When you do *(str + i) = 'M' you write to a random address.

You can do this to make the pointer version to work:
char my_string[6];
char *str = my_string;

tnx a lot for clear explaining.

In passing arrays to function, is there any difference between "void func (char * s)" and "void func (char s[])" ?

which one is better?
 
Last edited:

C text book (Kernighan/Ritchie) says both are equivalent. They prefer char *, indicating the pointer nature. In the examples, array notation is used where the function argument is accessed as character array and pointer notation with pointer access. But actually, both styles can be mixed.


Code C - [expand]
1
2
3
4
5
6
7
void copy(char to[], char from[])   
{       
  int i;
  i = 0;       
  while ((to[i] = from[i]) != '\0')
  ++i;
}



Code C - [expand]
1
2
3
4
5
6
7
int strlen(char *s)   
{       
  int n;
  for (n = 0; *s != '\0', s++)           
  n++;       
  return n;   
}

 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top