开发者

pointers in c (beginner)

开发者 https://www.devze.com 2023-02-25 08:49 出处:网络
I just started to look at C, coming from a java background. I\'m having a difficult time wrapping my head around pointers. In theory I feel like I get it but as soon as I try to use them or follow a p

I just started to look at C, coming from a java background. I'm having a difficult time wrapping my head around pointers. In theory I feel like I get it but as soon as I try to use them or follow a program that's using them I get lost pretty quickly. I was trying to follow a string concat exercise but it wasnt working so I stripped it down to some basic pointer practice. It complies with a warning conflicting types for strcat func开发者_运维问答tion and when I run it, crashes completly.

Thanks for any help

#include <stdio.h>
#include <stdlib.h>
/* strcat: concatenate t to end of s; s must be big enough */
void strcat(char *string, char *attach);


int main(){
char one[10]="test";
char two[10]="co";
char *s;
char *t;
s=one;
t=two;
strcat(s,t);
}

void strcat(char *s, char *t) {

    printf("%s",*s);




}


Your printf() should look like this:

printf("%s",s);

The asterisk is unnecessary. The %s format argument means that the argument should be a char*, which is what s is. Prefixing s with * does an extra invalid indirection.

You get the warning about conflicting types because strchr is a standard library routine, which should have this signature:

char * strcat ( char * destination, const char * source );

Yours has a different return type. You should probably rename yours to mystrchr or something else to avoid the conflict with the standard library (you may get linker errors if you use the same name).


Change

printf("%s",*s);

to

printf("%s",s);

The reason for this is printf is expecting a replacement for %s to be a pointer. It will dereference it internally to get the value.

Since you declared s as a char pointer (char *s), the type of s in your function will be just that, a pointer to a char. So you can just pass that pointer directly into printf.


In C, when you dereference a pointer, you get the value pointed to by the pointer. In this case, you get the first character pointed to by s. The correct usage should be:

printf( "%s", s );

BTW, strcat is a standard function that returns a pointer to a character array. Why make your own?


Replacing *s with s won't append strings yet, here is fully working code :


Pay attention to function urstrcat

#include <stdio.h>
#include <stdlib.h>
/* urstrcat: concatenate t to end of s; s must be big enough */
void urstrcat(char *string, char *attach);


int main(){
char one[10]="test";
char two[10]="co";
char *s;
char *t;
s=one;
t=two;
urstrcat(s,t);
return 0;
}

void urstrcat(char *s, char *t) {

    printf("%s%s",s,t);
}


pointers are variable which points to address of a variable.

#include "stdio.h"
void main(){
  int a,*b;
  a=10;
  b=&a;
  printf("%d",b);
}

in the follwing code you will see a int 'a' and a pointer 'b'.
here b is taken as pointer of an integer and declared by giving
'' before it.'' declare that 'b' is an pointer.then you will see "b=&a".this means b is taking address of integer "a" which is keeping value 10 in that particular memory and printf is printing that value.

0

精彩评论

暂无评论...
验证码 换一张
取 消