2

I want to take a substring of the string buffer by doing something like the below. I don't know if it's possible (I've been coding in C for all of about 6 hrs now, but feel free to be as technical as you like, I think I can handle it (though I may be wrong))

Edit: I want to take a substring of buffer from the beginning of buffer to the first space.

if (buffer[c] == ' ') {
    in_addr_t addr;
    char *ptr = *buffer;
    if(inet_aton("*ptr to *ptr+c", &addr)!=0){
           //do stuff;
    }
}
7
  • 1
    Is there some reason why strncpy, strncat and the other string functions won't work? I would answer the question, but it's not clear as to exactly what you want to happen. Commented Jan 21, 2010 at 5:26
  • 1
    Could you please describe in words what you are trying to accomplish in this code? I just don't get it from the code alone. Commented Jan 21, 2010 at 5:26
  • 1
    well I suppose I could use them. Part of the reason I'm asking is that I'm genuinely interested in understanding what I can and cannot do with pointers. Commented Jan 21, 2010 at 5:27
  • In your example, you're trying to assign *buffer to ptr, which should give you warning if it compiles at all; mixing and matching between scalar and pointer types is dangerous at best. Commented Jan 21, 2010 at 5:43
  • 1
    To get a thorough grounding in low level pointer diddling, read K&R and work the exercises. Really. It is a short book, and very clear (though you have to read it with exacting attention to detail). Commented Jan 21, 2010 at 16:08

2 Answers 2

4

I have to make one assumption since there are a number of problems with the code: Assuming that buffer[c] is the first character before the inet address

if (buffer[c] == ' ')
{
     in_addr_t addr
     if (inet_aton(&buffer[c+1], &addr))
          // do stuff
}

Note:

  • inet_aton is deprecated since it does not support ipv6. Use int inet_pton(int af, const char *src, void *dst); for forward compatibility.

-- Edit --
To take the substring from the beginning of buffer to (but not including) buffer[c], any of these will work:

1

char buf2 [MAX];
strncpy (buf2, buffer, c);
buf2 [c] = '\000';

2

char buf2 [MAX];
sprintf (buf2, "%.*s", c, buffer);

3

char buf2 [MAX];
int  j;
for (j = 0;  j < c;  ++j)
    buf2 [j] = buffer [j];
buf2 [c] = '\000';
Sign up to request clarification or add additional context in comments.

4 Comments

Could you explain the meaning of &buffer[c+1] a little? as I said, I've been using c for about 6 hrs. Also, I'm just trying to learn c at the moment so this isn't code that I will ever potentially use, but thanks for pointing that out.
&buffer[c+1] is equivalent to (buffer + c + 1). It's the address of entry c + 1 in the array.
buffer is a pointer; buffer[c] is a char (presumably). buffer[c+1] is also a char (the one after the space—which I'm assuming is where the inet address text begins—but the function needs a pointer to the first character to process. The address of buffer[c+1] is &buffer[c+1].
thanks a lot. I got it working and it feels beautiful. The only thing that pains me is that I had to artificially (using a break) exit my for loop.
2

If you can modify the original buffer, you could just ignore your ptr variable and do:

if (buffer[c] == ' ') {
    in_addr_t addr;
    buffer[c] = '\0';
    if (inet_aton(buffer, &addr) != 0) {
        // do stuff;
    }
}

If you can't modify the original buffer, just use strncpy() to copy the part you care about out into a new buffer.

5 Comments

can I then move the pointer to buffer to the beginning of the new string? like *buffer = *buffer+c or something like that?
Close; to do that you want buffer += c + 1. The +1 gets your pointer past the \0 and to the beginning of the new string. Be careful that you keep track of the original pointer if it matters (because you might need to free() it for example.)
and keeping track of the old pointer would just be char temp = buffer?
As an aside, your comment here and the original question seem to show a bit of overuse of the * operator; make sure you are understanding the difference between operations on the objects pointed to and operations on the pointers themselves.
@Mechko, almost. char *temp = buffer. You need a pointer type to save a copy of a pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.