Using pointer to array of chars in order to find memory adress of '

This is a discussion on Using pointer to array of chars in order to find memory adress of '

Thread: Using pointer to array of chars in order to find memory adress of '\0' character

' character
within the C Programming forums, part of the General Programming Boards category; hello again let's say we have an array of chars, and we want to find the memory adress of '
  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    Using pointer to array of chars in order to find memory adress of '\0' character

    hello again

    let's say we have an array of chars, and we want to find the memory adress of '\0' character, also we want to print it.

    this is what i did

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void) {
        
        char s1[] = "name";   
        int i;
        char *p;
        
        p = &s1;
        
        for (i=0;i<=strlen(s1);i++){
            
            if (*(p + i) != '\0') {
                   printf("not yet\n");
                   }
            else if (*(p + i) == '\0')
             {
                 printf("found \0 character\n");
                 
                 printf("memory adress: %d\n",p+i);
                 printf("character: %c\n",*(p+i));
                 }
                 }
    
        system("PAUSE");
        
        return 0;
    
    }
    my problem is that i get a warning, for line 11 which is this line

    Code:
     p = &s1;
    Code:
    11 C:\..\test.c [Warning] assignment from incompatible pointer type
    i think it works fine, but why am i getting this warning?

    thanks in advance

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    "p" is a char*, "s1" is a char* (arrays are essentially pointers). your trying to assign a "char**" (&s1) to a "char*" (p). Basically, change it to "p = s1".

    EDIT: This illustrates that "*(p + i)" is equivalent to "p[i]".
    Also, you have:
    Code:
    printf("found \0 character\n");
    You should escape this character and use "\\0".
    Last edited by nadroj; 10-31-2009 at 04:01 PM.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    thank you very much again

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by nadroj View Post
    "p" is a char*, "s1" is a char* (arrays are essentially pointers). your trying to assign a "char**" (&s1) to a "char*" (p). Basically, change it to "p = s1".

    EDIT: This illustrates that "*(p + i)" is equivalent to "p[i]".
    Also, you have:
    Code:
    printf("found \0 character\n");
    You should escape this character and use "\\0".
    That is incorrect. The actual type is char (*)[5], as illustrated in this error message:
    Error 1 error C2440: '=' : cannot convert from 'char (*)[5]' to 'char *'
    It is actually a pointer to an array of 5 elements, not a pointer to pointer.

    I also suggest you indent more correctly, something along the lines of:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void) {
    
    	char s1[] = "name";   
    	int i;
    	char *p;
    
    	p = &s1;
    
    	for (i=0;i<=strlen(s1);i++){
    
    		if (*(p + i) != '\0') {
    			printf("not yet\n");
    		}
    		else if (*(p + i) == '\0')
    		{
    			printf("found \0 character\n");
    
    			printf("memory adress: %d\n",p+i);
    			printf("character: %c\n",*(p+i));
    		}
    	}
    
    	system("PAUSE");
    
    	return 0;
    
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by nadroj
    "s1" is a char* (arrays are essentially pointers).
    To make Elysia's point more explicit: arrays are not essentially pointers. Rather, an array is converted to a pointer to its first element in many contexts.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM